Programming and Graphics

(Kiana) #1

9.4 Transferring data to theMatlabdomain 297


engPutVariable(iams, "hrn", hronos);

/*--- Reserve the array ‘‘position’’
Copy into memory
Transfer the data */

mxArray * position = mxCreateDoubleMatrix(1, sdata, mxREAL);
memcpy((void *)mxGetPr(position), (void *)y, sizeof(y));
engPutVariable(iams, "pos", position);

/*--- Reserve the array ‘‘distance’’
Evaluate in the Matlab domain */

mxArray * distance = mxCreateDoubleMatrix(1, sdata, mxREAL);
engEvalString(iams, "distance = 0.5*hrn.^2;");

/*--- Run a Matlab session:

engEvalString(iams, "plot(hrn,pos,’o-’);");
engEvalString(iams, "hold on;");
engEvalString(iams, "plot(hrn,distance,’rs:’);");
engEvalString(iams, "ylabel(’metrisis’,’fontsize’,15)");
engEvalString(iams, "xlabel(’hronos’,’fontsize’,15)");
engEvalString(iams, "set(gca,’fontsize’,15)");

/*--- End the session:

cout << "Hit return to finish" << endl;
fgetc(stdin);

return 0;
}

Figure 9.4.1 shows the generated graphics display. The graph can be saved in
a graphics file under various formats using menu options given in the graphics
window.


Three-dimensional graph


The following code contained in the filemplot3d.ccproduces a vector
containing nodes along thexaxis, and a second vector containing nodes along
theyaxis. The code then defines a two-dimensional Cartesian grid based on
thexandynodes, evaluates a function at the nodes, and finally prepares a
three-dimensional plot:

Free download pdf