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

8 views (last 30 days)
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

Accepted Answer

Salman Ahmed
Salman Ahmed on 1 Sep 2021
Edited: Salman Ahmed on 1 Sep 2021
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 Comment
Walter Roberson
Walter Roberson on 1 Sep 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!