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

7 Ansichten (letzte 30 Tage)
zhang
zhang am 10 Mär. 2014
Beantwortet: Image Analyst am 15 Mär. 2014
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?

Antworten (2)

dpb
dpb am 10 Mär. 2014
Read
doc system
more carefully and all shall be revealed...
  3 Kommentare
dpb
dpb am 11 Mär. 2014
Bearbeitet: dpb am 11 Mär. 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?
zhang
zhang am 12 Mär. 2014
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)])

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 15 Mär. 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);

Kategorien

Mehr zu Call C++ from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by