engEvalString not working as expected

2 ビュー (過去 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 Center および File ExchangeCall MATLAB from C についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by