Pass parameters to a .exe file and get the exe output data

7 次查看(过去 30 天)
Hi,
I called a exe file in matlab like this:
% code
myfun = [num2str(a),num2str(b)];
system(myfun)
myfun is a c++ program:
% code
double myfun(double a, double b)
{
return(a+b);
}
the question is how can I get the return value of "myfun" in matlab?

回答(2 个)

dpb
dpb 2014-3-10
Read
doc system
more carefully and all shall be revealed...
  3 个评论
dpb
dpb 2014-3-11
编辑:dpb 2014-3-11
A) What does "can not get the right result" mean, specifically? What's the problem you're actually having other than "doesn't work?"
B) What is the actual command to be executed? What does your string from
['dlltest.exe ', num2str(123)]
give? Will the code run from the command line with that string?
C) What is the result of status and result you are getting?
zhang
zhang 2014-3-12
the right command is ['dlltest.exe ' num2str(123)] I can get the right result with this c++ program:
void main(int argc, char* argv[])
{
float x = 2*atof(argv[1]);
std::cout<<x<<std::endl;
}
and now the x is returned to the"result" parameter in matlab [status result]=system(['dlltest.exe ' num2str(123)])

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-3-15
I don't understand this:
myfun = [num2str(a),num2str(b)]; % Create string from numbers.
system(myfun) % Operating system runs this number.
So "a" and "b" are two numbers, say 13 and 42. And you're going to turn that into a string, like '1342' and stuff that into a string variable called (unfortunately) myfun (unfortunate because it's the same name as your C++ program which is misleading and deceptive). Then you're going to issue that number to the operating system's command line like it's a program you want to run. So it's as if you typed 1342 at the command prompt of the console window.
First of all, I don't think that a program can be a number.
Secondly, just because you call that string myfun DOES NOT mean that issuing system(myfun), which remember is like system('1342'), will call your myfun C++ program.
You need to create your program name, and arguments with sprintf() and then call system. Look at this example where I launch Photoshop with the name of an image file:
% Check to see that Photoshop executable exists.
editorFullFileName = 'C:\Program Files\Adobe\Adobe Photoshop CS5 (64 Bit)\Photoshop.exe';
if ~exist(editorFullFileName, 'file')
errorMessage = sprintf('Cannot find the Photoshop program.\n%s', editorFullFileName);
uiwait(warndlg(errorMessage));
return;
end
% Now run the Photoshop program, passing in the image filename.
% First construct the command line for the system() function.
% Enclose all filenames in double quotes because we may have spaces in the filenames.
arguments = sprintf('"%s"', fullImageFileName);
commandLine = sprintf('"%s" %s', editorFullFileName, arguments);
fprintf('%s', commandLine);
% Now launch the Photoshop program using the "system()" function.
system(commandLine);

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by