Call matlab from c++ using VS2010 and matlab 2007a

0

I am trying to call matlab from c++ with code hereunder. I send matlab commands from c++ through engEvalString and I want to get the output from matlab, but I got this error:

Unhandled exception at 0x78531986 in EnginetEST.exe: 0xC0000005: Access violation reading location 0x00000024.

please help.

int main()

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

Engine *ep;

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

}

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

aN[0] = 23; aw_net[0]=1; aQoS[0]=2; asecurity[0]=3; acost[0]=4; auserR[0]=5; aRSS[0]=6; aBW[0]=7; aNQoS[0]=8; acoverage[0]=9; aUtil[0]=10; adelay[0]=11; aBER[0]=12; aPacketLoss[0]=13; aNetworkP[0]=14; acomp[0]=15; adist[0]=16; abattery[0]=17; avelocity[0]=18; adevicecond[0]=19; aR_time[0]=20; aNon_R_time[0]=21; aAppType[0]=22; atarget[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); cost=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(cost), (void *)acost, sizeof(double)*1); engPutVariable(ep, "acost", cost);

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);

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

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

delay=mxCreateDoubleMatrix(1, 1, mxREAL); memcpy((void *)mxGetPr(delay), (void *)adelay, sizeof(double)*1); engPutVariable(ep, "adealy", delay);

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);

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

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

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);

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

  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 security cost], userR)= 1;"); engEvalString(ep, " dag([delay BER PacketLoss],NQoS)=1;"); engEvalString(ep, " dag([BW RSS coverage NQoS Util], NetworkP)=1;"); engEvalString(ep, " dag([comp dist battery velocity], devicecond)=1;"); engEvalString(ep, " dag([R_time Non_R_time],AppType)=1;"); engEvalString(ep, " dag([userR NetworkP devicecond AppType],target)=1;"); engEvalString(ep, " discrete_nodes = [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, " onodes = [];"); engEvalString(ep, " bnet = mk_bnet(dag, node_sizes,'names',{'w_net','QoS','security', 'cost' ,'userR', 'RSS', 'BW', 'NQoS','coverage','Util', 'delay', >'BER', 'PacketLoss' ,'NetworkP','comp', 'dist', 'battery','velocity', 'devicecond', 'R_tim e', 'Non_R_time', 'AppType', 'target'},'discrete', discrete_nodes, 'observed', onodes);");

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, "engine = jtree_inf_engine(bnet);"); engEvalString(ep, "evidence = cell(1,N);");

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

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

myfile << "LTE=" << cresult[0]; myfile.close();

  engClose(ep);

return EXIT_SUCCESS; }

c++
matlab
asked on Stack Overflow Jul 30, 2013 by nasser • edited Aug 28, 2013 by Dennis Jaheruddin

1 Answer

0

This is probably very late reply but this sample code of mine may help you.

#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;

}
answered on Stack Overflow Jul 16, 2014 by Pranav

User contributions licensed under CC BY-SA 3.0