Set Gain parameters from Code

6 views (last 30 days)
Hi,
I am defining some parameters in Appdesigner that will be used to simulate a simulink model.
I have the variables from input data
K1
K2
K3
K4
that will be stored as a gain matrix [K1 K2 K3 K4]
This gain matrix need to overwrite the values in my simulink model, but my code only seems to change when I enter values inside ' '.
K1 = app.K1Slider.Value;
K2 = app.K2Slider.Value;
K3 = app.K3Slider.Value;
K4 = app.K4Slider.Value;
mdl = 'Pendulo_Team_Simios';
open_system(mdl);
in = Simulink.SimulationInput(mdl);
set_param('Pendulo_Team_Simios/KLQR','Gain',[K1 K2 K3 K4])
Any idea how to fix this?
If i put set_param('Pendulo_Team_Simios/KLQR','Gain','[K1 K2 K3 K4]') the gain block literally goes [K1 K2 K3 K4], but not with the numerical values

Answers (1)

Harikrishnan Balachandran Nair
Hi,
I understand that you are trying to set the value of 'Gain' parameter of a block in your simulink model ,from app designer, using the 'set_param' function.
Using the 'set_param' Function, if you assign the variable name to the 'value' string , the same string will appear in the corresponding Gain block in the simulink Model. Hence, if there is no variable in the base/model workspace with the same name, it will result in error.
One possible solution to avoid this would be to add a variable with the same name and value to the base workspace using the 'assignin' function.
For example, in this case, inside the same function in app designer, you can add the following lines of code .
assignin('base','K1',K1);
assignin('base','K2',K2);
assignin('base','K3',K3);
assignin('base','K4',K4);
Hope this helps!

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!