How can I have a MCC-compiled application return a specific exit code?

10 次查看(过去 30 天)
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

回答(2 个)

William Smith
William Smith 2018-2-15
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 个评论
Bogdan Dzyubak
Bogdan Dzyubak 2021-8-5
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
Oleg Krivosheev 2024-2-8
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
Image Analyst 2013-1-14
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 个评论
Bogdan Dzyubak
Bogdan Dzyubak 2021-8-5
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
Image Analyst 2021-8-5
@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.

请先登录,再进行评论。

类别

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