engEvalString not working as expected

조회 수: 5 (최근 30일)
User05
User05 2015년 3월 10일
답변: User05 2015년 3월 12일
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