Using matlab command from VS2013/C++ system() call to exeute a script - fprintf issue

1 回表示 (過去 30 日間)
Hello.
I have a basic MATLAB script to read a value from a txt file, process the value (simple multiply) then output the result to a file. If I the call following from Windows cmd line, it works as expected i.e. I see the output file generated:
C:\Users\Me\Documents\MATLAB>matlab -r matlab_test -wait
If I call the following from a basic VS2013 C++ App, MATLAB opens and appears to execute the script but no output file is created:
int32_t status = system("\"C:\\Program Files\\MATLAB\\R2015a\\bin\\matlab\" -r matlab_test -wait");
Note 'status' returns 0.
The following is the MATLAB script (matlab_test.m):
fileIP = fopen('input_data.txt','r'); fileOP = fopen('output_data.txt','w');
a = fscanf( fileIP, '%d' ); b = 2; c = a * b;
fprintf( fileOP, '%d\n', c );
fclose( fileIP ); fclose( fileOP );
quit;
I am using MATLAB 2015a/64-bit.
  1 件のコメント
Steven Bridges
Steven Bridges 2015 年 4 月 16 日
Update:
So it seems the output file is generated but is created in a different location than executing from Windows command line. When executing 'matlab' command from Win cmd line, the output file is located in the same folder as the MATLAB script. When executing 'matlab' command from VS/C++ app, it is located in the VS project folder.

サインインしてコメントする。

採用された回答

Ken Atwell
Ken Atwell 2015 年 4 月 17 日
You self-diagnosed well. Windows' working folder is different in the two scenarios. When you start MATLAB not from the Start Menu, the initial folder will be the working folder of wherever you started it from.
There is possibly too much information about this topic in the documentation.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by