how to manage 'Switch case' and persistent variables in Matlab Function Block in Simulink?

조회 수: 5(최근 30일)
Amjad MOUHAIDALI
Amjad MOUHAIDALI 2021년 7월 4일
댓글: Walter Roberson 2021년 9월 1일
Dear community,
I want to simulate a model in Simulink using Matlab Function. The model has two inputs and two outputs. And there are several variables in the Matlab function code.
In the first part of the code, I want to initialize the variables. And in the second part I want to calculate the outputs depending on the inputs and the variables. Some variables are persistent.
I tried two solutions, but they are note working.
  • The sketch of the code is as follows:
function [output1,output2] = fcn(flag,input1, input2)
persistent a b
switch flag
case 1
% load variables from .mat file, for example a,b,c,d,x and y
% some allocations
% do it only once for Initialization.
case 2
% Regular time step
a = a*x+c;
b = b*y+d;
output1 = a*input1;
output2 = b*input2;
end
I want to know how to use the switch statement. What to put in the flag? Is it a vector or a Step from Simulink?
  • I tried to use the InitFcn callback in the Matlab function block properties. But it is not working. I can see the variables in the base workspace, but Simulink is giving me an error that the variables are not defined. I tried to define the variable as global but is also not working. In this solution I also tried to put all the variables in one object and to enter it as a parameter to the block as described here (https://fr.mathworks.com/help/simulink/ug/parameter-arguments-in-matlab-function-block-functions.html). But I do not know how to manage the persistent variables.
I need your help to find the best solution.
Thanking you, Amjad

채택된 답변

Salman Ahmed
Salman Ahmed 2021년 9월 1일
편집: Salman Ahmed 2021년 9월 1일
Hi Amjad,
I understand you are facing some issues with the MATLAB function block. Have a look at the following sample code to see if it solves your problem:
function [output1,output2] = fcn(flag,input1, input2)
persistent a b
% Checking if the persitent variables are empty or not
if isempty(a)||isempty(b)
a=1; b=1; %Assign some default values.
end
% Assigning default values to the variables in the function
% Note outputs also have to be defined under all scenarios
x=1; y=1; c=1; d=1; output1=1; output2=1;
% Use the flag for loading different values or updating
switch flag
case 1
% load variables from .mat file, for example a,b,c,d,x and y
% some allocations
% do it only once for Initialization.
case 2
% Regular time step
a = a*x+c;
b = b*y+d;
output1 = a*input1;
output2 = b*input2;
end
It is important to have the persitent variables initialized properly. Unlike in the switch case, the output values also need to be known at all times. Also, in response to your flag question, the flag can be used with a manual switch between two constants 1 and 2. You may choose to reload variables or outputs with case choice 1 and update on choice 2.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 9월 1일
Note that Simulink must be able to see that obviously all used variables are assigned values. Salman Ahmed's process of checking isempty() and assigning values works fine for that purpose: Simulink knows the ideom that persistent variables start out empty.
The problem with not doing that isempty() check, and relying on the switch flag case 1 to do the initialization, is that Simulink cannot prove that case 1 will definitely be executed first before any case that uses the variables is processed. Simulink does not know the idiom that some kinds of S functions pass in that calling sequence but other kinds of S functions have different initialization sequences. The limits of Simulink's reasoning seeing the isempty() and knowing that means that the variables will definitely be initialized to something.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by