2013-06-04 37 views
1

當模型模擬時,我想在simulink塊中設置增益(K值)。如何在仿真過程中從GUI設置simulink模型參數?

enter image description here

我創建了一個GUI包含一個按鈕和編輯文本(標記的「text_box」),並且該按鈕的回調函數將設置在底座的工作空間指定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通過

% --- 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); 

改變K的SIMULINK也試過,但我會得到錯誤說:

Error using robotics_gui_2>FK_button_Callback (line 429) 
Invalid setting in Gain block 'Gain' for parameter 'Gain' 

我能做些什麼來改變K的SIMULINK在模擬?

回答

4

您需要調用set_param來更新Gain塊使用的值。它不會自動從MATLAB工作區讀取新值。您傳遞給set_param的值是一個字符串。因此,在set_param中使用它之前,不需要將其轉換爲double。您也可以使用set_param('gui_variable/Gain','Gain', 'K')這將使set_param從工作區中讀取K的新值。

相關問題