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

7 views (last 30 days)
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).

Answers (2)

Robert Cumming
Robert Cumming on 27 Oct 2014
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 Comment
cmcnabb
cmcnabb on 27 Oct 2014
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.

Sign in to comment.


Geoff Hayes
Geoff Hayes on 27 Oct 2014
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.)

Categories

Find more on C Shared Library Integration 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!