Clear Filters
Clear Filters

Try/Catch inside the Simulink Function

22 views (last 30 days)
Mohammadreza Chalak Qazani
Answered: Abhas on 6 Aug 2024 at 8:21
I am using the Simulink of MATLAB 2017a. There is a function inside the Simulink using lqr code inside the function.
The lqr function is very sensitive and it can not generate the information with every set of weights. Then, I am trying to use try and catch to skipe the situation that lqr can not find the feasible solution.
But problem is that I can not use the try/catch inside the Simulink Function. I have faced with error which says "code generation does not support TRY/CATCH construction". Any idea how can i use try/catch inside the Simulink Function.
If you are recomending "coder.extrinsic" what should I put instead of the function name? Because I have already tried this and it does not work.
  3 Comments
Syed Ali Zaryab
Syed Ali Zaryab on 30 Nov 2020
I am facing the same problem. Did you find a way to resolve it?
Roop
Roop on 25 Jun 2024
Still facing a similar issue

Sign in to comment.

Answers (1)

Abhas
Abhas on 6 Aug 2024 at 8:21
Hi,
When working with Simulink in MATLAB, the "try/catch" construct is not supported for code generation even when using "coder.extrinsic". A viable workaround is to create a separate MATLAB script or function that handles the "lqr" computation and error handling, and then call this script or function from the MATLAB Function block using "coder.extrinsic".
You can follow the below steps to achieve this:
  • Create a new MATLAB function file, e.g., calculateLqr.m:
function [K, S, e] = calculateLqr(A, B, Q, R)
% Initialize outputs
K = [];
S = [];
e = [];
% Try to compute the LQR
try
[K, S, e] = lqr(A, B, Q, R);
catch
% Handle the error case
K = NaN;
S = NaN;
e = NaN;
end
end
  • Open the MATLAB Function Block Editor in your Simulink model and replace the code with the following:
function [K, S, e] = myLqrFunction(A, B, Q, R)
% Declare outputs
K = zeros(size(B'));
S = zeros(size(A));
e = zeros(size(A,1),1);
% Declare the external function as extrinsic
coder.extrinsic('calculateLqr');
% Call the external function
[K, S, e] = calculateLqr(A, B, Q, R);
end
  • Set the values of the "Constant blocks" to appropriate matrices for 'A', 'B', 'Q', and 'R'.
The "calculateLqr" function will be executed in interpreted mode, and the "try/catch" block within it will handle any errors that occur when "lqr" is unable to find a feasible solution.
You may refer to the following MathWorks documentation links to have a better understanding on coder.extrinsi, lqr function and different simulation modes of Simulink:
  1. coder.extrinsic: https://www.mathworks.com/help/simulink/ug/using-extrinsic-functions.html
  2. lqr: https://www.mathworks.com/help/control/ref/lqr.html
  3. simulation modes: https://www.mathworks.com/help/simulink/ug/simulation-modes.html

Tags

Products

Community Treasure Hunt

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

Start Hunting!