Passing a string variable between my c++ code to matlab
4 ビュー (過去 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 :)
0 件のコメント
採用された回答
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!