Are there any alternatives to try/catch statements that are supported by code generation?
35 ビュー (過去 30 日間)
古いコメントを表示
I am trying to prepare an application (prepared in App Designer and connected to a Simulink model) to be packaged as a standalone executable.
My Simulink model is fairly simple: using Interpreted MATLAB function blocks as input and output to an s-function block (which is connected to a much larger and complicated external model).
In order to create a standalone executable for an application connected to a Simulink model, I found that I have to use Simulink Compiler, which does not support Interpreted MATLAB functions, so I am trying to convert their functionality to MATLAB function blocks.
As it turns out, MATLAB function blocks are further subject to code generation limitations, which exclude try/catch statements. These try/catch statements are primarily used to catch and pass error code or correct user input with instructions, which is a core functionality of deploying a proprietary application in industry.
Therefore, I was wondering if there are any alternatives to try/catch statements that are supported by code generation? I could not find any simple fix in the documentation. Are there any options besides coding in more input validation?
Thanks!
3 件のコメント
dpb
2024 年 6 月 25 日
Unfortunately, if you can't use try...catch construct, then ensuring the error doesn't happen first is really the only alternative because if you let any error happen, BOOM! goes the app and if's then too late to do anything; the horse has already left the barn.
About all you can do to help is probably write one or more helper functions that let you screen inputs for validity(*); the issue happens if such screening needs a lot of additional logice to be effective if you can't just select a range of valid inputs.
Of course, that also gets into how you let the user actually interact with your app, if they can type freely into a text field and you then convert to a number, the problem is much more complex than if it's a slider (which would be the way one could go for input that must be in a range to avoid/prevent the error if it is that simple).
(*) One of my generic utility functions is simply "syntactic sugar" to replace the logic construct in the top level user code with a function is
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
This has the advantage of being able to be combined in logic combinations without the muss of the actual comparison to try to read plus is vectorized. Things such as this customized for your specific needs can help, but it's still going to be a lot of work to make something that is very robust unless you can otherwise control the way the user interacts with the app.
採用された回答
Paul
2024 年 6 月 25 日
Hi Roop,
Can you post the code of a simple Matab Function that illustrates how you're tyring to use a try/catch? I'm having trouble envisioning how such might be used in the context of a Simulink model.
In the interim, I think that one can use error inside a Matlab function. Maybe you can detect the error inside the Matab Function, call error, and catch the error outside of Simulink in the App.
4 件のコメント
Paul
2024 年 6 月 26 日
It seems to work.
Generate a simple model
bdclose('all');
hsys = new_system('errtest');
hconstant = add_block('simulink/Sources/Constant','errtest/Input');
hfunc = add_block('simulink/User-Defined Functions/MATLAB Function','errtest/func');
hout = add_block('simulink/Sinks/To Workspace','errtest/output');
PH = 'PortHandles';
add_line(hsys, ...
get_param(hconstant,PH).Outport,...
get_param(hfunc,PH).Inport);
add_line(hsys, ...
get_param(hfunc,PH).Outport, ...
get_param(hout,PH).Inport);
c = get(hfunc,'MATLABFunctionConfiguration');
c.FunctionScript = strrep(c.FunctionScript,'y = u','y = u; error(''foobar'')');
% Uncomment this line to open the model on the Simulink canvas for by-hand editing, execution, etc.
% open_system(hsys);
The exception generated from inside Simulink is caught (check ME.message)
try
out = sim('errtest');
catch ME
ME
end
However, the try/catch may not be needed. We can also do this, which is probably a better approach.
out = sim('errtest','CaptureErrors','on');
out
out.ErrorMessage
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Schedule Model Components についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!