フィルターのクリア

How to set simulink model parameter from GUI during simulation?

4 ビュー (過去 30 日間)
Po-Jen
Po-Jen 2013 年 6 月 4 日
コメント済み: rangan 2014 年 7 月 22 日
I want to set the gain(value of K) in the simulink block when this model is simulating.
I created a GUI contains a button and an edit text(tagged "text_box"), and the callback function of this button will set the gain by specifying the K in base work space.
% --- Executes on button press in FK_button.
function FK_button_Callback(hObject, eventdata, handles)
% hObject handle to FK_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global K;
text=get(handles.text_box, 'String');
value = str2double(text);
K = value;
However, the simulink block only read the value of K in work space when I started simulation. During the simulation, if I press the button, the K value in base work space would change to the value I set, but the value of K in simulink wouldn't change.
I've also tried using set_param API to change the K in simulink by
% --- Executes on button press in FK_button.
function FK_button_Callback(hObject, eventdata, handles)
% hObject handle to FK_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global K;
text=get(handles.text_box, 'String');
value = str2double(text);
%gui_variable is the model file name
set_param('gui_variable/Gain','Gain', value);
But I'll got error saying:
Error using robotics_gui_2>FK_button_Callback (line 429)
Invalid setting in Gain block 'Gain' for parameter 'Gain'
What can I do to alter K in simulink during simulation?
  1 件のコメント
rangan
rangan 2014 年 7 月 22 日
I had the same problem but solved it using this
value = get(hObject,'String')
string = value{1} // Converting cell to string
set_param([bdroot '/Gain'],'Gain',string)

サインインしてコメントする。

採用された回答

Ilham Hardy
Ilham Hardy 2013 年 6 月 4 日
set_param('gui_variable/Gain','Gain', value);
Is value string?
  2 件のコメント
Po-Jen
Po-Jen 2013 年 6 月 4 日
No, it's double. Should I use string?
Ilham Hardy
Ilham Hardy 2013 年 6 月 4 日
Yes

サインインしてコメントする。

その他の回答 (1 件)

Chandrasekhar
Chandrasekhar 2013 年 6 月 4 日
In the callback function 'function FK_button_Callback(hObject, eventdata, handles)' write the following code assignin('base', 'K', val); // val will be the value that you want to give for the gain.
the gain block in the Simulink model will read the K value from the workspace.
  4 件のコメント
Po-Jen
Po-Jen 2013 年 6 月 4 日
I've tried before I left the comment, but it doesn't work. It's effect is the same as using K = value; in my callback function.
Kaustubha Govind
Kaustubha Govind 2013 年 6 月 4 日
Po-Jen: Is the model running when the value of K changes in the base workspace? If yes, you need to update the model using set_param('modelname', 'SimulationCommand', 'update') to force Simulink to notice the new value.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by