Passing a string variable between my c++ code to matlab

3 次查看(过去 30 天)
I am new to matlab, and coding is not my job, I just use it for some side projects. So I don't really know what I am talking about, and I hope you'll understand that :)
So I installed matlab on my computer and would like to use its libraries to plot some very simple graphs during the execution of my code (histograms, scatter plots, whatever). Plotting those graphs is not the first purpose of my code, I just find that easier to plot them during the execution rather than exporting them as a CSV file, and then plotting manually through excel.
Question: I managed to make visual C++ "communicate" with matlab. I am passing some data using arrays, but I'd also like to pass a string (a path such as "C:\test\") as I'd like to automatically save those graphs once generated into a precise directory. I haven't found any way of doing it so far.
Here is a bit of my c++ code, which is really simple:
Engine *ep;
ep = engOpen(NULL);
double *ArrayOne;
double *ArrayTwo;
const int Asize = Area.size();
ArrayOne = new double[Asize];
ArrayTwo = new double[Asize];
for (int i = 0; i <= Area.size() - 1; i++) {
ArrayOne[i] = Area[i][1];
ArrayTwo[i] = Area[i][2]
}
mxArray* ONE = mxCreateDoubleMatrix(Asize, 1, mxREAL);
memcpy((void*)mxGetPr(ONE), (void*)ArrayOne, sizeof(double)*Asize);
engPutVariable(ep, "one", ONE);
mxArray* TWO = mxCreateDoubleMatrix(Asize, 1, mxREAL);
memcpy((void*)mxGetPr(TWO), (void*)ArrayTwo, sizeof(double)*Asize);
engPutVariable(ep, "two", TWO);
engEvalString(ep, "plottest");
delete[]ArrayOne;
delete[]ArrayTwo;
engClose(ep);
And the file Plottest.m:
h1= histogram(one);
h1.EdgeColor = 'black';
h1.FaceColor = 'white';
hold on;
h2 = histogram(two);
h2.EdgeColor = 'blue';
h2.FaceColor = [0.5 0.5 0.5];
alpha(h1,.5);
alpha(h2,.8);
saveas(gcf,'C:\PhD\SVG2GMSH\SVG\test.png');
How can I replace my hard coded path into my m file ("C:\PhD\SVG2GMSH\SVG\test.png") by a more elegant variable that would contain it ? I precise that I'd like to do it that way as a new folder is created each time I run my C++ code, and I'd like my plot to be dynamically saved into that folder.
Thank you for your help. Also, let me know if you have any other suggestions in order to make my code look/work better :)

采纳的回答

James Tursa
James Tursa 2016-4-18
First, once you are done using the mxArray variables, destroy them. E.g., at the end of your code do this:
mxDestroyArray(TWO);
mxDestroyArray(ONE);
To pass a string to MATLAB, use the mxCreateString API function. E.g.,
char mypath[] = "C:\PhD\SVG2GMSH\SVG\test.png";
:
mxArray* MYPATH = mxCreateString(mypath);
engPutVariable(ep, "mypath", MYPATH);
mxDestroyArray(MYPATH);
Another thing you might consider is only copying your data once instead of twice. E.g.,
double *ArrayOne;
double *ArrayTwo;
const int Asize = Area.size();
mxArray* ONE = mxCreateDoubleMatrix(Asize, 1, mxREAL);
mxArray* TWO = mxCreateDoubleMatrix(Asize, 1, mxREAL);
ArrayOne = mxGetPr(ONE);
ArrayTwo = mxGetPr(TWO);
for (int i = 0; i < Asize; i++) {
ArrayOne[i] = Area[i][1];
ArrayTwo[i] = Area[i][2]
}
engPutVariable(ep, "one", ONE);
engPutVariable(ep, "two", TWO);
mxDestroyArray(TWO);
mxDestroyArray(ONE);
// delete[]ArrayOne; <-- Not needed anymore
// delete[]ArrayTwo; <-- Not needed anymore

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by