engEvalString not working as expected

3 views (last 30 days)
User05
User05 on 10 Mar 2015
Answered: User05 on 12 Mar 2015
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

Accepted Answer

User05
User05 on 12 Mar 2015
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.

More Answers (1)

James Tursa
James Tursa on 11 Mar 2015
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 Comments
James Tursa
James Tursa on 11 Mar 2015
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 on 11 Mar 2015
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!