0

我正在尝试使用下面的代码从 c++ 调用 matlab。我通过 engEvalString 从 c++ 发送 matlab 命令,我想从 matlab 获取输出,但出现此错误:

EnginetEST.exe 中 0x78531986 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000024。

请帮忙。

主函数()

{ ofstream myfile; myfile.open("example.txt");

引擎 *ep;

  if (!(ep = engOpen("\0"))) {
  fprintf(stderr, "\nCan't start MATLAB engine\n");
  return EXIT_FAILURE;

}

双 aN[1]、aw_net[1]、aQoS[1]、asecurity[1]、acost[1]、auserR[1]、aRSS[1]、aBW[1]、aNQoS[1]、acoverage[1] ; 双 aUtil[1]、adelay[1]、aBER[1]、aPacketLoss[1]、aNetworkP[1]、acomp[1]、adist[1]、battery[1]、avelocity[1];双 adevicecond[1]、aR_time[1]、aNon_R_time[1]、aAppType[1]、atarget[1];

aN[0] = 23;aw_net[0]=1;aQoS[0]=2;安全[0]=3;成本[0]=4;用户R[0]=5;aRSS[0]=6;aBW[0]=7;aNQoS[0]=8;覆盖[0]=9;aUtil[0]=10;延迟[0]=11;aBER[0]=12;aPacketLoss[0]=13;aNetworkP[0]=14;acomp[0]=15;adist[0]=16;电池[0]=17;速度[0]=18;adevicecond[0]=19;aR_time[0]=20;aNon_R_time[0]=21;aAppType[0]=22;目标[0]= 23;

mxArray *N, *w_net, *QoS, *security, *cost, *userR, *RSS, *BW, *NQoS, *coverage, *Util, *delay, *BER, *PacketLoss, *NetworkP; mxArray *comp、*dist、*battery、*velocity、*devicecond、*R_time、*Non_R_time、*AppType、*target;N=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(N), (void *)aN, sizeof(double)*1); engPutVariable(ep, "aN", N);

w_net=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(w_net), (void *)aw_net, sizeof(double)*1); engPutVariable(ep,“aw_net”,w_net);成本=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(cost), (void *)acost, sizeof(double)*1); engPutVariable(ep,“acost”,成本);

userR=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(userR), (void *)auserR, sizeof(double)*1); engPutVariable(ep,“auserR”,userR);

RSS=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(RSS), (void *)aRSS, sizeof(double)*1); engPutVariable(ep,“aRSS”,RSS);

BW=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(BW), (void *)aBW, sizeof(double)*1); engPutVariable(ep, "aBW", BW);

NQoS=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(NQoS), (void *)aNQoS, sizeof(double)*1); engPutVariable(ep,“aNQoS”,NQoS);

覆盖=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(coverage), (void *)acoverage, sizeof(double)*1); engPutVariable(ep,“覆盖”,覆盖);

Util=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(Util), (void *)aUtil, sizeof(double)*1); engPutVariable(ep, "aUtil", Util);

延迟=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(delay), (void *)adelay, sizeof(double)*1); engPutVariable(ep,“adealy”,延迟);

BER=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(BER), (void *)aBER, sizeof(double)*1); engPutVariable(ep,“aBER”,BER);

PacketLoss=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(PacketLoss), (void *)aPacketLoss, sizeof(double)*1); engPutVariable(ep,“aPacketLoss”,PacketLoss);

NetworkP=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(NetworkP), (void *)aNetworkP, sizeof(double)*1); engPutVariable(ep,“aNetworkP”,NetworkP);

comp =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(comp), (void *)acomp, sizeof(double)*1); engPutVariable(ep,“acomp”,comp);

dist =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(dist), (void *)adist, sizeof(double)*1); engPutVariable(ep, "adist", dist);

电池 =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(电池), (void *)电池, sizeof(double)*1); engPutVariable(ep,“电池”,电池);

速度 =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(velocity), (void *)avelocity, sizeof(double)*1); engPutVariable(ep,“速度”,速度);

devicecond =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(devicecond), (void *)adevicecond, sizeof(double)*1); engPutVariable(ep, "adevicecond", devicecond);

R_time =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(R_time), (void *)aR_time, sizeof(double)*1); engPutVariable(ep, "aR_time", R_time);

Non_R_time =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(Non_R_time), (void *)aNon_R_time, sizeof(double)*1); engPutVariable(ep, "aNon_R_time", Non_R_time);

AppType =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(AppType), (void *)aAppType, sizeof(double)*1); engPutVariable(ep, "aAppType", AppType);

目标 =mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(target), (void *)atarget, sizeof(double)*1); engPutVariable(ep,“目标”,目标);

  engEvalString(ep, " dag = zeros(N,N);");

engEvalString(ep, " dag(w_net,[security PacketLoss BER Battery QoS cost delay >BW RSS coverage R_time Non_R_time]) =1;"); engEvalString(ep, " dag([QoS 安全成本], userR)= 1;"); engEvalString(ep, " dag([delay BER PacketLoss],NQoS)=1;"); engEvalString(ep, " dag([BW RSS 覆盖 NQoS Util], NetworkP)=1;"); engEvalString(ep, " dag([comp dist 电池速度], devicecond)=1;"); engEvalString(ep, "dag([R_time Non_R_time],AppType)=1;"); engEvalString(ep, "dag([userR NetworkP devicecond AppType],target)=1;"); engEvalString(ep,“离散节点= [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23];”);engEvalString(ep, "node_sizes = [3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2];"); engEvalString(ep,“

engEvalString(ep, "CPT_w_net = zeros(3,1);"); engEvalString(ep, "CPT_w_net(1) = 0.3; CPT_w_net(2) = 0.2;CPT_w_net(3) = 0.5;");

engEvalString(ep, "CPT_cost = zeros(3,2);"); engEvalString(ep, " CPT_cost(1,1) = 0.2; CPT_cost(1,2) = 0.8;"); engEvalString(ep, " CPT_cost(2,1) = 0.4; CPT_cost(2,2) = 0.6;"); engEvalString(ep, " CPT_cost(3,1) = 0.6; CPT_cost(3,2) = 0.4;");

engEvalString(ep, "CPT_QoS = zeros(3,2);"); engEvalString(ep, " CPT_QoS(1,1) = 0.2; CPT_QoS(1,2) = 0.8;"); engEvalString(ep, " CPT_QoS(2,1) = 0.4; CPT_QoS(2,2) = 0.6;"); engEvalString(ep, " CPT_QoS(3,1) = 0.8; CPT_QoS(3,2) = 0.2;");

engEvalString(ep, "CPT_security = zeros(3,2);"); engEvalString(ep, " CPT_security(1,1) = 0.2; CPT_security(1,2) = 0.8;"); engEvalString(ep, " CPT_security(2,1) = 0.6; CPT_security(2,2) = 0.4;"); engEvalString(ep, " CPT_security(3,1) = 0.7; CPT_security(3,2) = 0.3;");

engEvalString(ep, " CPT_userR = zeros(2,2,2,2);"); engEvalString(ep, " CPT_userR(1,1,1,1) = 0.9; CPT_userR(1,1,1,2) = 0.8;"); engEvalString(ep, " CPT_userR(1,1,2,1) = 0.1; CPT_userR(1,1,2,2) = 0.2;"); engEvalString(ep, " CPT_userR(1,2,1,1) = 0.8; CPT_userR(1,2,1,2) = 0.4;"); engEvalString(ep, " CPT_userR(1,2,2,1) = 0.2; CPT_userR(1,2,2,2) = 0.6;"); engEvalString(ep, " CPT_userR(2,1,1,1) = 0.8; CPT_userR(2,1,1,2) = 0.4;"); engEvalString(ep, " CPT_userR(2,1,2,1) = 0.2; CPT_userR(2,1,2,2) = 0.6;"); engEvalString(ep, " CPT_userR(2,2,1,1) = 0.4; CPT_userR(2,2,1,2) = 0.1;"); engEvalString(ep, " CPT_userR(2,2,2,1) = 0.6; CPT_userR(2,2,2,2) = 0.9;");

engEvalString(ep, "CPT_BER = zeros(3,2);"); engEvalString(ep, "CPT_BER(1,1) = 0.6; CPT_BER(1,2) = 0.4;"); engEvalString(ep, "CPT_BER(2,1) = 0.5; CPT_BER(2,2) = 0.5;"); engEvalString(ep, "CPT_BER(3,1) = 0.3; CPT_BER(3,2) = 0.7;");

engEvalString(ep, "CPT_PacketLoss = zeros(3,2);"); engEvalString(ep, "CPT_PacketLoss (1,1) = 0.6; CPT_PacketLoss (1,2) = 0.4;"); engEvalString(ep, "CPT_PacketLoss (2,1) = 0.5; CPT_PacketLoss (2,2) = 0.5;"); engEvalString(ep, " CPT_PacketLoss (3,1) = 0.4; CPT_PacketLoss (3,2) = 0.6;");

engEvalString(ep, "CPT_delay = zeros(3,2);"); engEvalString(ep, " CPT_delay (1,1) = 0.7; CPT_delay (1,2) = 0.3;"); engEvalString(ep, " CPT_delay (2,1) = 0.5; CPT_delay (2,2) = 0.5;"); engEvalString(ep, " CPT_delay (3,1) = 0.3; CPT_delay (3,2) = 0.7;");

engEvalString(ep, "CPT_NQoS = zeros(2,2,2,2);"); engEvalString(ep, " CPT_NQoS(1,1,1,1) = 0.1; CPT_NQoS(1,1,1,2) = 0.2;"); engEvalString(ep, "CPT_NQoS(1,1,2,1) = 0.9; CPT_NQoS(1,1,2,2) = 0.8;"); engEvalString(ep, "CPT_NQoS(1,2,1,1) = 0.2; CPT_NQoS(1,2,1,2) = 0.4;"); engEvalString(ep, "CPT_NQoS(1,2,2,1) = 0.8; CPT_NQoS(1,2,2,2) = 0.6;"); engEvalString(ep, "CPT_NQoS(2,1,1,1) = 0.2; CPT_NQoS(2,1,1,2) = 0.4;"); engEvalString(ep, " CPT_NQoS(2,1,2,1) = 0.8; CPT_NQoS(2,1,2,2) = 0.6;"); engEvalString(ep, "CPT_NQoS(2,2,1,1) = 0.4; CPT_NQoS(2,2,1,2) = 0.8;"); engEvalString(ep, "CPT_NQoS(2,2,2,1) = 0.6; CPT_NQoS(2,2,2,2) = 0.2;");

engEvalString(ep, " CPT_BW = zeros(3,2);CPT_BW(1,1) = 0.3; CPT_BW(1,2) = 0.7;CPT_BW(2,1) = 0.6; CPT_BW(2,2) = 0.4; CPT_BW(3,1) = 0.4; CPT_BW(3,2) = 0.6;");

engEvalString(ep, " CPT_RSS = zeros(3,2);CPT_RSS(1,1) = 0.6; CPT_RSS(1,2) = 0.4;CPT_RSS(2,1) = 0.6; CPT_RSS(2,2) = 0.4; CPT_RSS(3,1) = 0.5; CPT_RSS(3,2) = 0.5;");

engEvalString(ep, " CPT_coverage = zeros(3,2);CPT_coverage(1,1) = 0.3; CPT_coverage(1,2) = 0.7;CPT_coverage(2,1) = 0.7; CPT_coverage(2,2) = 0.3; CPT_coverage(3,1) = 0.9; CPT_coverage(3,2) = 0.1;");

engEvalString(ep, " CPT_Util = zeros(2,1); CPT_Util(1) = 0.5; CPT_Util(2) = 0.5;");

engEvalString(ep, " CPT_NetworkP = zeros(2,2,2,2,2,2); CPT_NetworkP(1,1,1,1,1,1) = 0.9; CPT_NetworkP(1,1,1,1,1 ,2) = 0.8; CPT_NetworkP(1,1,1,1,2,1) = 0.1; CPT_NetworkP(1,1,1,1,2,2) = 0.2;");

engEvalString(ep, " CPT_NetworkP(1,1,1,2,1,1) = 0.8; CPT_NetworkP(1,1,1,2,1,2) = 0.7; CPT_NetworkP(1,1,1,2,2 ,1) = 0.2; CPT_NetworkP(1,1,1,2,2,2) = 0.3; CPT_NetworkP(1,1,2,1,1,1) = 0.8; CPT_NetworkP(1,1,2,1, 1,2) = 0.8;");

engEvalString(ep, " CPT_NetworkP(1,1,2,1,2,1) = 0.2; CPT_NetworkP(1,1,2,1,2,2) = 0.2;CPT_NetworkP(1,1,2,2,1 ,1) = 0.7; CPT_NetworkP(1,1,2,2,1,2) = 0.5;CPT_NetworkP(1,1,2,2,2,1) = 0.3; CPT_NetworkP(1,1,2,2, 2,2) = 0.5;");

engEvalString(ep, " CPT_NetworkP(1,2,1,1,1,1) = 0.8; CPT_NetworkP(1,2,1,1,1,2) = 0.7;CPT_NetworkP(1,2,1,1,2 ,1) = 0.2; CPT_NetworkP(1,2,1,1,2,2) = 0.3;CPT_NetworkP(1,2,1,2,1,1) = 0.7; CPT_NetworkP(1,2,1,2, 1,2) = 0.5;");

engEvalString(ep, " CPT_NetworkP(1,2,1,2,2,1) = 0.3; CPT_NetworkP(1,2,1,2,2,2) = 0.5; CPT_NetworkP(1,2,2,1,1 ,1) = 0.7; CPT_NetworkP(1,2,2,1,1,2) = 0.6; CPT_NetworkP(1,2,2,1,2,1) = 0.3; CPT_NetworkP(1,2,2,1, 2,2) = 0.4;");

engEvalString(ep, " CPT_NetworkP(1,2,2,2,1,1) = 0.4; CPT_NetworkP(1,2,2,2,1,2) = 0.3; CPT_NetworkP(1,2,2,2,2 ,1) = 0.6; CPT_NetworkP(1,2,2,2,2,2) = 0.7;");

engEvalString(ep, " CPT_NetworkP(2,1,1,1,1,1) = 0.8; CPT_NetworkP(2,1,1,1,1,2) = 0.6; CPT_NetworkP(2,1,1,1,2 ,1) = 0.2; CPT_NetworkP(2,1,1,1,2,2) = 0.4;CPT_NetworkP(2,1,1,2,1,1) = 0.7; CPT_NetworkP(2,1,1,2, 1,2) = 0.5;");

engEvalString(ep, " CPT_NetworkP(2,1,1,2,2,1) = 0.3; CPT_NetworkP(2,1,1,2,2,2) = 0.5;CPT_NetworkP(2,1,2,1,1 ,1) = 0.5; CPT_NetworkP(2,1,2,1,1,2) = 0.5;CPT_NetworkP(2,1,2,1,2,1) = 0.5; CPT_NetworkP(2,1,2,1, 2,2) = 0.5;"); engEvalString(ep, " CPT_NetworkP(2,1,2,2,1,1) = 0.4; CPT_NetworkP(2,1,2,2,1,2) = 0.4; CPT_NetworkP(2,1,2,2,2 ,1) = 0.6; CPT_NetworkP(2,1,2,2,2,2) = 0.6; CPT_NetworkP(2,2,1,1,1,1) = 0.5; CPT_NetworkP(2,2,1,1, 1,2) = 0.5;"); engEvalString(ep, " CPT_NetworkP(2,2,1,1,2,1) = 0.5; CPT_NetworkP(2,2,1,1,2,2) = 0.5; CPT_NetworkP(2,2,1,2,1 ,1) = 0.4; CPT_NetworkP(2,2,1,2,1,2) = 0.3; CPT_NetworkP(2,2,1,2,2,1) = 0.6; CPT_NetworkP(2,2,1,2, 2,2) = 0.7;"); engEvalString(ep, " CPT_NetworkP(2,2,2,1,1,1) = 0.4; CPT_NetworkP(2,2,2,1,1,2) = 0.2; CPT_NetworkP(2,2,2,1,2 ,1) = 0.6; CPT_NetworkP(2,2,2,1,2,2) = 0.8; CPT_NetworkP(2,2,2,2,1,1) = 0.3; CPT_NetworkP(2,2,2,2,1,2) = 0.1; "); engEvalString(ep, " CPT_NetworkP(2,2,2,2,2,1) = 0.7; CPT_NetworkP(2,2,2,2,2,2) = 0.9;");

engEvalString(ep, "CPT_battery = zeros(3,2);"); engEvalString(ep, " CPT_battery (1,1) = 0.4; CPT_battery (1,2) = 0.6;"); engEvalString(ep, " CPT_battery (2,1) = 0.3; CPT_battery(2,2) = 0.7;"); engEvalString(ep, " CPT_battery (3,1) = 0.8; CPT_battery(3,2) = 0.2;");

engEvalString(ep, "CPT_velocity = zeros(2,1);"); engEvalString(ep, "CPT_velocity(1) = 0.5; CPT_velocity(2) = 0.5;");

engEvalString(ep, "CPT_dist = zeros(2,1);"); engEvalString(ep, "CPT_dist(1) = 0.5; CPT_dist(2) = 0.5;");

engEvalString(ep, "CPT_comp = zeros(2,1);"); engEvalString(ep, "CPT_comp(1) = 0.5; CPT_comp(2) = 0.5;");

engEvalString(ep, "CPT_velocity = zeros(2,1);"); engEvalString(ep, "CPT_velocity(1) = 0.5; CPT_velocity(2) = 0.5;");

engEvalString(ep, " CPT_devicecond = zeros(2,2,2,2,2);"); engEvalString(ep, " CPT_devicecond(1,1,1,1,1) = 0.5; CPT_devicecond(1,1,1,1,2) = 0.3; CPT_devicecond(1,1,1,2,1) = 0.5; CPT_devicecond(1,1,1,2,2) = 0.7; CPT_devicecond(1,1,2,1,1) = 0.6; CPT_devicecond(1,1,2,1,2) = 0.8;"); engEvalString(ep, " CPT_devicecond(1,1,2,2,1) = 0.4; CPT_devicecond(1,1,2,2,2) = 0.2; CPT_devicecond(1,2,1,1,1) = 0.3; CPT_devicecond(1,2,1,1,2) = 0.7; CPT_devicecond(1,2,1,2,1) = 0.7; CPT_devicecond(1,2,1,2,2) = 0.3;"); engEvalString(ep, " CPT_devicecond(1,2,2,1,1) = 0.6; CPT_devicecond(1,2,2,1,2) = 0.9; CPT_devicecond(1,2,2,2,1) = 0.4; CPT_devicecond(1,2,2,2,2) = 0.1; CPT_devicecond(2,1,1,1,1) = 0.1; CPT_devicecond(2,1,1,1,2) = 0.4;"); engEvalString(ep, " CPT_devicecond(2,1,1,2,1) = 0.9; CPT_devicecond(2,1,1,2, 2) = 0.6; CPT_devicecond(2,1,2,1,1) = 0.6; CPT_devicecond(2,1,2,1,2) = 0.5; CPT_devicecond(2,1,2,2,1) = 0.4; CPT_devicecond(2,1,2,2,2) = 0.5;"); engEvalString(ep, " CPT_devicecond(2,2,1,1,1) = 0.5; CPT_devicecond(2,2,1,1,2) = 0.4;CPT_devicecond(2,2,1,2,1) = 0.5; CPT_devicecond(2,2,1,2,2) = 0.6;CPT_devicecond(2,2,2,1,1) = 0.7; CPT_devicecond( 2,2,2,1,2) = 0.6;"); engEvalString(ep, " CPT_devicecond(2,2,2,2,1) = 0.3; CPT_devicecond(2,2,2,2,2) = 0.4 ;");

engEvalString(ep, "CPT_R_time = zeros(3,2);"); engEvalString(ep, " CPT_R_time(1,1) = 0.5; CPT_R_time(1,2) = 0.5;CPT_R_time(2,1) = 0.3; CPT_R_time(2,2) = 0.7; CPT_R_time(3,1) = 0.2; CPT_R_time(3,2) = 0.8;");

engEvalString(ep, "CPT_Non_R_time = zeros(3,2);"); engEvalString(ep, " CPT_Non_R_time(1,1) = 0.4; CPT_Non_R_time(1,2) = 0.6; CPT_Non_R_time(2,1) = 0.3; CPT_Non_R_time(2,2) = 0.7; CPT_Non_R_time(3,1) = 0.3; CPT_Non_R_time(3,2) = 0.7;");

engEvalString(ep, "CPT_AppType = zeros(2,2,2);"); engEvalString(ep, " CPT_AppType(1,1,1) = 0.2; CPT_AppType(1,1,2) = 0.4;CPT_AppType(1,2,1) = 0.8; CPT_AppType(1,2,2) = 0.6; CPT_AppType (2,1,1) = 0.4; CPT_AppType(2,1,2) = 0.8;"); engEvalString(ep, " CPT_AppType(2,2,1) = 0.6; CPT_AppType(2,2,2) = 0.2;");

engEvalString(ep, "CPT_target = zeros(2,2,2,2,2);"); engEvalString(ep, " CPT_target(1,1,1,1,1) = 0.2; CPT_target(1,1,1,1,2) = 0.6;CPT_target(1,1,1,2,1) = 0.8; CPT_target(1,1,1,2,2) = 0.4; CPT_target(1,1,2,1,1) = 0.3; CPT_target(1,1,2,1,2) = 0.5;"); engEvalString(ep, " CPT_target(1,1,2,2,1) = 0.7; CPT_target(1,1,2,2,2) = 0.5; CPT_target(1,2,1,1,1) = 0.3; CPT_target(1,2,1,1,2) = 0.7; CPT_target(1,2,1,2,1) = 0.7; CPT_target(1,2,1,2,2) = 0.3;"); engEvalString(ep, " CPT_target(1,2,2,1,1) = 0.4; CPT_target(1,2,2,1,2) = 0.2; CPT_target(1,2,2,2,1) = 0.6; CPT_target(1,2,2,2,2) = 0.8;");

engEvalString(ep, " CPT_target(2,1,1,1,1) = 0.2; CPT_target(2,1,1,1,2) = 0.3; CPT_target(2,1,1,2,1) = 0.8; CPT_target(2,1,1,2,2) = 0.7; CPT_target(2,1,2,1,1) = 0.2; CPT_target(2,1,2,1,2) = 0.1;"); engEvalString(ep, " CPT_target(2,1,2,2,1) = 0.8; CPT_target(2,1,2,2,2) = 0.9; CPT_target(2,2,1,1,1) = 0.5; CPT_target(2,2,1,1,2) = 0.5; CPT_target(2,2,1,2,1) = 0.5; CPT_target(2,2,1,2,2) = 0.5;"); engEvalString(ep, " CPT_target(2,2,2,1,1) = 0.4; CPT_target(2,2,2,1,2) = 0.6;CPT_target(2,2,2,2,1) = 0.6; CPT_target(2,2,2,2,2) = 0.4;"); engEvalString(ep, " bnet.CPD{w_net} = tabular_CPD(bnet, w_net, CPT_w_net);"); engEvalString(ep, "bnet.CPD{QoS} = tabular_CPD(bnet, QoS, CPT_QoS);"); engEvalString(ep, " bnet.CPD{security} = tabular_CPD(bnet, security, CPT_security);"); engEvalString(ep, " bnet.CPD{cost} = tabular_CPD(bnet, cost, CPT_cost);"); engEvalString(ep, "bnet.CPD{userR} = tabular_CPD(bnet,userR, CPT_userR);"); engEvalString(ep, " bnet.CPD{RSS} = tabular_CPD(bnet,RSS, CPT_RSS);");
engEvalString(ep, " bnet.CPD{BW} = tabular_CPD(bnet,BW, CPT_BW);"); engEvalString(ep, " bnet.CPD{NQoS} = tabular_CPD(bnet,NQoS, CPT_NQoS);"); engEvalString(ep, " bnet.CPD{coverage} = tabular_CPD(bnet,coverage, CPT_coverage);"); engEvalString(ep, " bnet.CPD{Util} = tabular_CPD(bnet,Util, CPT_Util);"); engEvalString(ep, " bnet.CPD{delay} = tabular_CPD(bnet, delay, CPT_delay);"); engEvalString(ep, " bnet.CPD{BER} = tabular_CPD(bnet, BER, CPT_BER);"); engEvalString(ep, " bnet.CPD{PacketLoss} = tabular_CPD(bnet, PacketLoss, CPT_PacketLoss);"); engEvalString(ep, " bnet.CPD{NetworkP} = tabular_CPD(bnet, NetworkP, CPT_NetworkP);"); engEvalString(ep, " bnet.CPD{comp} = tabular_CPD(bnet, comp, CPT_comp);"); engEvalString(EP," bnet.CPD{dist} = tabular_CPD(bnet, dist, CPT_dist);"> engEvalString(ep, " bnet.CPD{battery} = tabular_CPD(bnet, battery, CPT_battery);"); engEvalString(ep, " bnet.CPD{velocity} = tabular_CPD(bnet, velocity, CPT_velocity);"); engEvalString(ep, "bnet.CPD{devicecond} = tabular_CPD(bnet, devicecond, CPT_devicecond);"); engEvalString(ep, " bnet.CPD{R_time} = tabular_CPD(bnet, R_time, CPT_R_time);"); engEvalString(ep, " bnet.CPD{Non_R_time} = tabular_CPD(bnet, Non_R_time, CPT_Non_R_time);"); engEvalString(ep, "bnet.CPD{target} = tabular_CPD(bnet, target, CPT_target);"); engEvalString(ep, "bnet.CPD{AppType} = tabular_CPD(bnet, AppType, CPT_AppType);"); engEvalString(ep, "bnet.CPD{devicecond} = tabular_CPD(bnet, devicecond, CPT_devicecond);"); engEvalString(ep, " bnet.CPD{R_time} = tabular_CPD(bnet, R_time, CPT_R_time);"); engEvalString(ep, " bnet.CPD{Non_R_time} = tabular_CPD(bnet, Non_R_time, CPT_Non_R_time);"); engEvalString(ep, "bnet.CPD{target} = tabular_CPD(bnet, target, CPT_target);"); engEvalString(ep, "bnet.CPD{AppType} = tabular_CPD(bnet, AppType, CPT_AppType);"); engEvalString(ep, "bnet.CPD{devicecond} = tabular_CPD(bnet, devicecond, CPT_devicecond);"); engEvalString(ep, " bnet.CPD{R_time} = tabular_CPD(bnet, R_time, CPT_R_time);"); engEvalString(ep, " bnet.CPD{Non_R_time} = tabular_CPD(bnet, Non_R_time, CPT_Non_R_time);"); engEvalString(ep, "bnet.CPD{target} = tabular_CPD(bnet, target, CPT_target);"); engEvalString(ep, "bnet.CPD{AppType} = tabular_CPD(bnet, AppType, CPT_AppType);");

engEvalString(ep, "engine = jtree_inf_engine(bnet);"); engEvalString(ep, "证据 = cell(1,N);");

engEvalString(ep, "[engine, loglik] = enter_evidence(engine, evidence);"); engEvalString(ep, "marg = margin_nodes(engine,w_net);");

双 *cresult; mxArray *mresult; engEvalString(ep, "z = marg.T(1);"); mresult = engGetVariable(ep,"z"); cresult = mxGetPr(mresult);

我的文件 << "LTE=" << cresult[0]; 我的文件.close();

  engClose(ep);

返回 EXIT_SUCCESS;}

4

1 回答 1

0

这可能是很晚的回复,但我的这个示例代码可能会对您有所帮助。

#include<iostream>
#include<math.h>
#include<engine.h>
#pragma comment (lib, "libmat.lib")
#pragma comment (lib, "libmx.lib")
#pragma comment (lib, "libmex.lib")
#pragma comment (lib, "libeng.lib")
using namespace std;

const int N = 9;                //Number of grid points


int main()
{
    // start matlab engine
    Engine *ep = engOpen(NULL);     
    if (!(ep = engOpen(""))) {      
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }

    int i, j;
// Grid creation with aspect ratio r
    double sum_r = 0, r = 1;
    for (i = 0; i < N-1; i++){
        sum_r += pow(r,i);
    }
    double dx0 = 1 / sum_r;         // calculate first dx
    double dy0 = 1 / sum_r;         // calculate first dy

    double x[N][N], y[N][N];        //define grid variable
    double dx, dy;

    for (j = 0; j < N; j++){
        x[0][j] = 0; x[1][j] = dx0;
        y[0][j] = 0; y[1][j] = dy0;
        for (i = 2; i < N; i++){
            dx = x[i - 1][j] - x[i - 2][j];
            dy = y[i - 1][j] - y[i - 2][j];
            x[i][j] = x[i - 1][j] + r*dx;
            y[i][j] = y[i - 1][j] + r*dy;
        }
    }

    mxArray *x_matlab = mxCreateDoubleMatrix(N, N, mxREAL);
    double *pa = mxGetPr(x_matlab);

    memcpy(pa, *x, N*sizeof(double)*N);
    engPutVariable(ep, "x", x_matlab);
    engEvalString(ep, "x = x';");

    double* ans = NULL;
    mxArray *result = NULL;
    result = engGetVariable(ep, "x");
    mwSize nRow = mxGetM(result);
    mwSize nCol = mxGetN(result);
    ans = mxGetPr(result);

    for (int j = 0; j < N; j++){
        for (int i = 0; i < N; i++){
            y[i][j] = ans[i * N + j];
        }
        cout << endl;
    }

    system("pause");
    mxDestroyArray(x_matlab);
    engClose(ep);
    return 0;

}
于 2014-07-16T19:08:53.653 回答