How to update Simulink model parameters during a run without using signals?

7 views (last 30 days)
Tamir Moshe
Tamir Moshe on 30 Sep 2021
I have a Simulink model with blocks like: Gain, Saturation, Integrator, MATLAB Function, Constant etc.. with model workspace's parameters containing the values of the different properties of those blocks.
In addition, the Simulink model contain a state machine implemented as a Stateflow chart.
I want the project parameters, in the model workspace, to change to different sets of predetermined values for each state of the state machine.
I was told that it is not possible to access the model workspace's parameters during a run, and there are too many possible states with different sets of parameters values to make copies of sections of the Simulink model with different parameters values.
The only option that I could think of to change those parameters during the run is by replacing the different blocks containing the parameters with MATLAB Function blocks that perform the same operations and have the parameters values as input signals which will be entered by signals from a Data Store Memory block that is connected to the Stateflow chart of the state machine.
This solution prevents me from using basic Simulink blocks and instead requires the remaking of those blocks using MATLAB Function blocks, moreover the parameters values in this solution are updated by signals which makes the Simulink model far more complicated because of the big number of parameters and it is very redundant, because each signal contain the unnecessary information of a parameter value for each time of the run.
Are there any other ways to define parameters for a Simulink model and change them from a Stateflow chart during the run without using signals?

Answers (1)

Harikrishnan Balachandran Nair
Edited: Harikrishnan Balachandran Nair on 20 Oct 2021
Hi,
I understand that you are trying to change the block parameters in your model, during the simulation, depending on the state reached in your stateflow chart.
You can use the 'set_param' function to set the parameter of specified object in the model. For example, in your model named 'model', you can set the Gain value of block named 'Gain1' to 10 using the following line of code :
set_param('model/Gain1','Gain','10');
You can execute this line of code, on reaching the corresponding state, to change the block parameters depending on the state reached.
You might want to add the line 'coder.extrinsic('set_param')' before executing the 'set_param' function, if you get an error stating that the function is not supported for code generation.
However, it is not recommended to change the block parameters during the execution.
You may refer to this link to know why :

Community Treasure Hunt

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

Start Hunting!

Translated by