How do I force a compiled function to exit with a non-zero status yet allow the overall application to continue to run.

조회 수: 9 (최근 30일)
I have an application that executes several compiled functions using the 'system' command...
e.g.
status1 = system('mycompiledfnc1.exe');
if status1==0 then
status2 = system('mycompiledfnc.exe');
end
I want to force 'status1' to a nonzero value under certain conditions (e.g. nonexistent file) but I am not able to do that. I have tried throwing an error using:
error('my error message')
... but that just seems to throw the error and then freeze execution of the overall application. If I trap the error and exit more gracefully it just returns status=0 (normal execution). Is there a way to force the return 'status' to a nonzero value while allowing the overall application to continue to execute? (fyi... I am using version 2014b).

답변 (2개)

Robert Cumming
Robert Cumming 2014년 10월 27일
You want to in effect transfer information from one exe to another. Is that correct?
Since you are using the system command there is not much you can do, you could parse the message returned (this is stuff written to terminal window)
A better way would be to save information from exe1 and then load it in exe2. Assuming you have access to the source code of both....
  댓글 수: 1
cmcnabb
cmcnabb 2014년 10월 27일
Thanks. that sounds like a viable work around. However; I still wonder if it is possible to programatically manipulate the status returned by the system command. That would seem to be a cleaner solution and an appropriate use of the 'status' result.

댓글을 달려면 로그인하십시오.


Geoff Hayes
Geoff Hayes 2014년 10월 27일
You haven't mentioned how you build/compile your compiled function executable, but if it is written in C/C++, then you may be able to do something like the following which makes use of its return value. If I write a very simple program like
#include <stdio.h>
int main(int argc, char *argv[])
{
return argc;
}
it just returns the number of input arguments. Saving the above to a file called main.c and compiling (outside of MATLAB) as
gcc main.c -o main.exe
then I can run this function using MATLAB's system as
>> system('./main.exe')
ans =
1
>> system('./main.exe 1')
ans =
2
>> system('./main.exe 1 ''string''')
ans =
3
So as the number of inputs increase, the return value of the function increases as well. So in this instance, we are able to programmatically manipulate the status returned by system. (If your code generates an error, then just return one or some other non-zero value.)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by