我想在模拟该模型时在simulink块中设置增益(K值) .
我创建了一个包含按钮和编辑文本(标记为“text_box”)的GUI,此按钮的回调函数将通过在基本工作空间中指定K来设置增益 .
% --- 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;
但是,当我开始模拟时,simulink块只读取工作空间中的K值 . 在模拟过程中,如果按下按钮,基本工作空间中的K值将更改为我设置的值,但simulink中的K值不会更改 .
我也尝试使用set_param API在simulink中更改K.
% --- 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);
但我会说错误:
Error using robotics_gui_2>FK_button_Callback (line 429)
Invalid setting in Gain block 'Gain' for parameter 'Gain'
在模拟过程中,我可以做什么来改变simulink中的K?
1 回答
您需要调用set_param来更新Gain块使用的值 . 它不会自动从MATLAB工作空间读取新值 . 传递给set_param的值是一个字符串 . 因此,在set_param中使用它之前,不需要将其转换为double . 您还可以使用
set_param('gui_variable/Gain','Gain', 'K')
,这将使set_param从工作空间读取K的新值 .