[英]How to insert Bode Plot function in a Matlab GUI
我正在創建一個GUI,該GUI根據輸入的數據來繪制波特圖。 我有以下代碼,但它給了我一個我不明白的錯誤。
function first_gui
%This gui plots a bode plot from a
%a Transfer function generated from the main plot
%Create a figure with the plot and others pushbutons
f = figure('Visible','on','Position',[360,500,600,400]);
hplot = uicontrol('Style','pushbutton','String','Plot','Position',[415,200,70,25],'Callback',@tf_Callback);
%Create an entering data to numerator
htext = uicontrol('Style','text','String','Entre com a função de transferência','Position',[320,350,250,15]);
hnum = uicontrol(f,'Style','edit','String','Enter TF numerator...','Position',[320,320,250,20]);
%Create an entering data to denominator
htext_2 = uicontrol('Style','text','String','Entre com a função de transferência','Position',[320,280,250,15]);
hden = uicontrol(f,'Style','edit','String','Enter TF denominator...','Position',[320,250,250,20]);
hfig = axes('Units','pixels','Position',[50,60,200,185]);
%Initialize the UI
f.Units = 'normalized';
hfig.Units = 'normalized';
hplot.Units = 'normalized';
hnum.Units = 'normalized';
hden.Units = 'normalized';
sys = tf(hnum,hden);
f.Name = 'Bode Plot';
%Function to plot Bode
function tf_Callback(source,eventdata)
bode(sys)
end
end
在IDLE上出現以下錯誤:
使用tf時出錯(第279行)“ tf”命令的語法無效。 鍵入“ help tf”以獲取更多信息。
Simple_Plot中的錯誤(第29行)sys = tf(hnum,hden);
未定義的函數或變量“ sys”。
Simple_Plot / tf_Callback(第36行)bode(sys)中的錯誤
評估uicontrol回調時出錯
您看到的錯誤是由於您對tf
的調用失敗而導致的,因此sys
從未得到定義。 然后在您的回調( tf_Callback
)中嘗試使用sys
但是由於從未創建過sys
,所以找不到它,並且出現了第二個錯誤。
因此,讓我們看一下傳遞給tf
以了解失敗的原因。 您通過了hden
和hnum
。 您以這種方式創建它們。
hden = uicontrol('style', 'edit', ...);
hnum = uicontrol('style', 'edit', ...);
這會將MATLAB 圖形對象分配給變量hden
。 該對象本身可用於操縱該對象的外觀並設置/獲取其值 。 對於編輯框, String
屬性包含在框中實際鍵入的內容。 因此,我懷疑您實際上想要傳遞給tf
是hden
和hnum
uicontrols的值 ,而不是句柄本身。 因此,您必須自己獲取值並將其轉換為數字( str2double
)。
hden_value = str2double(get(hden, 'String'));
hnum_value = str2double(get(hnum, 'String'));
然后,您可以將這些值傳遞給tf
。
sys = tf(hnum_value, hden_value);
現在應該可以了。 但是,我相信您真正想要的是當用戶單擊“繪圖”按鈕時從那些編輯框中檢索值。 您目前所擁有的方式,由於值位於回調函數之外,因此僅會檢索一次(在GUI啟動時)。 如果你希望他們每一個 “陰謀”按鈕被點擊的時間來抓取用戶提供的值,那么你會希望把你的按鈕回調( 在上面的代碼tf_Callback
)。
function tf_Callback(src, evnt)
hden_value = str2double(get(hden, 'String'));
hnum_value = str2double(get(hnum, 'String'));
sys = tf(hnum_value, hden_value);
bode(sys);
end
現在,每次用戶單擊按鈕時,將從編輯框中檢索值,將計算sys
並創建波特圖。
您可能要向回調添加一些其他錯誤檢查,以確保為hden
和hnum
輸入的值有效,並且將生成有效的圖,並可能引發警告( warndlg
)或錯誤( errordlg
)以警告用戶它們選擇的無效值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.