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

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?

Answers (2)

Read
doc system
more carefully and all shall be revealed...

3 Comments

Hi,Thanks for your reply! i still have some problems,wish you can help me!
the matlab program is like this:
[status result]=system(['dlltest.exe ', num2str(123)])
the parameters passed to the exe file is string, so I must change the string to number in the exe file, but I have tried many times and can not get the right result,can you help me? The exe file is generate with c++
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?
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)])

Sign in to comment.

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);

Asked:

on 10 Mar 2014

Answered:

on 15 Mar 2014

Community Treasure Hunt

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

Start Hunting!