Set Gain parameters from Code

조회 수: 9 (최근 30일)
Luis Francisco Sanchez
Luis Francisco Sanchez 2021년 11월 23일
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

답변 (1개)

Harikrishnan Balachandran Nair
Harikrishnan Balachandran Nair 2021년 11월 30일
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!

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by