Try/Catch inside the Simulink Function

조회 수: 8 (최근 30일)
Mohammadreza Chalak Qazani
Mohammadreza Chalak Qazani 2019년 10월 24일
답변: Abhas 2024년 8월 6일
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
Syed Ali Zaryab
Syed Ali Zaryab 2020년 11월 30일
I am facing the same problem. Did you find a way to resolve it?
Roop
Roop 2024년 6월 25일
Still facing a similar issue

댓글을 달려면 로그인하십시오.

답변 (1개)

Abhas
Abhas 2024년 8월 6일
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

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by