Pass parameters to a .exe file and get the exe output data
7 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
dpb
on 10 Mar 2014
Read
doc system
more carefully and all shall be revealed...
3 Comments
dpb
on 11 Mar 2014
Edited: dpb
on 11 Mar 2014
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?
Image Analyst
on 15 Mar 2014
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);
0 Comments
See Also
Categories
Find more on Call C++ from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!