How can I have a MCC-compiled application return a specific exit code?
2 views (last 30 days)
Show older comments
Hi,
I would like to use a compiled MATLAB application (.exe) as a tool that is called from another application. This is a simple compiled function (single .m file), not an application with GUI. So no callback functions such as yourApp_OutputFcn(hObject, eventdata, handles) are available, I believe.
How can I set the exit code associated with the resulting .exe application inside the MATLAB script, so I can communicate with the calling application using this exit code (e.g., signal success/failure via the exit code)?
Can one of the output parameters of the compiled MATLAB function be used as the exit code? How does mcc set the exit code of the application that it generates?
Thanks for your help, Daniel
0 Comments
Answers (2)
William Smith
on 15 Feb 2018
Compiled Matlab programs automatically exit 0 if the main function returns, and -1 if an error occurs prior to this. Maybe that's all you need?
2 Comments
Bogdan Dzyubak
on 5 Aug 2021
If I throw an error, I do get the -1 error code, but it also creates a popup window. Any idea how to avoid this?
Oleg Krivosheev
on 8 Feb 2024
Well, Windows PowerShell expects return codes to be within [0...256) range. Not sure how -1 will be shown - as 255, I guess?
Image Analyst
on 14 Jan 2013
In the output function, for example yourApp_OutputFcn(), set varargout to be the returned values.
% --- Outputs from this function are returned to the command line.
function varargout = yourApp_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
varargout{1} = 42;
Then call your gui. When it exits, it will return whatever you set in varargout{1}.
4 Comments
Bogdan Dzyubak
on 5 Aug 2021
This doesn't appear to work as a true error code. A function like this when compiled on Matlab 2014b and run returns 0 instead of the expected -1:
function error_code = my_function()
try
class
error_code = 0;
catch
error_code = 1;
end
cmd> echo Exit Code is %errorlevel%
0
While the below does give the intended effect, it also creates a popup window. Any suggestions?
function error_code = my_function()
class
Image Analyst
on 5 Aug 2021
@Bogdan Dzyubak, class() is a function that needs you to pass it a variable so it can tell you what kind of variable it is (its class). You did not pass it anything, hence the error about class needing input arguments.
Also, your function never sets anything ever to -1 so I don't know why you expect -1. In fact your function throws an error when it hits the class line so error_code will never be set at all, and that will probably lead to a second error about your function not returning a value.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!