engEvalString not working as expected

3 次查看(过去 30 天)
Hi,
I am trying to call a MATLAB 2014b test script from my Visual Studio 2013 project. The project builds and runs fine without any error, but I think its not calling/executing my MATLAB script. Below is my C++ code snippet:
int main()
{
Engine *ep;
if (!(ep = engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
engEvalString(ep, "test");
printf("Hit return to continue\n\n");
fgetc(stdin);
engClose(ep);
return EXIT_SUCCESS;
}
test.m is suppose to create a plot, but I dont see any output plot when I run this code. I thought that the MATLAB Engine is not able to find the file and hence I added the path to MATLAB path as below:
engEvalString(ep, "addpath C:\Users\Projects\MATLAB");
engEvalString(ep, "test");
But the above also doesnt show the correct result. It looks like command engEvalString is not getting executed on MATLAB.
Any thoughts?
Thanks

采纳的回答

User05
User05 2015-3-12
I found the answer to my question.
1) To resolve addpath issue, I had to use "\\" instead of "\"
engEvalString(ep, "addpath(genpath('C:\\Users\\Projects\\MATLAB'))")
instead of
engEvalString(ep, "addpath(genpath('C:\Users\Projects\MATLAB'))")
2) For access denied issue, I had to cd into that path first and then the program ran fine.

更多回答(1 个)

James Tursa
James Tursa 2015-3-11
When the project is running and paused with the "Hit return to continue" message, the Engine window should be open. Click on that window and type "test" in the command line and see what happens (i.e., just try to manually run the test script).
  3 个评论
James Tursa
James Tursa 2015-3-11
For the path problem, try the function form of the call instead, e.g.,
engEvalString(ep, "addpath('C:\Users\Projects\MATLAB')");
For the Access problem, you will have to determine what that is actually referring to (directory access?, file access? file already open? etc) and then proceed from there.
User05
User05 2015-3-11
I figured out the access issue. It was because my pwd was not set correctly. After setting that, the access problem is resolved. But I still have the function call problem. My code looks like below:
engEvalString(ep, "addpath(genpath('C:\Users\Projects\MATLAB'))");
engEvalString(ep, "cd('C:\Users\Projects\MATLAB')");
engEvalString(ep, "test");
But this still doesnt seem to set the path correctly. If I manually do the above in the command prompt, everything works fine.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from C 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by