2016-03-08 74 views
0

我正在創建一個GUI,用於繪製輸入數據中的Bode Plot。我有下面的代碼,但它給了我一個我不明白的錯誤。如何在Matlab GUI中插入Bode Plot函數

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行) 波特(SYS)

錯誤而評估uicontrol回調

回答

0

您看到的錯誤是由於這樣的事實,你對tf調用失敗結果,sys永遠不會被定義。然後在你的回調中(tf_Callback),你嘗試使用sys,但是因爲它從未被創建過,所以找不到它並且你得到了第二個錯誤。

那麼讓我們來看看你傳遞給tf的內容,看看它爲什麼會失敗。您通過hdenhnum。你用這種方式創建它們。

hden = uicontrol('style', 'edit', ...); 
hnum = uicontrol('style', 'edit', ...); 

此分配一個MATLAB graphics object到可變hden。這個對象本身可以用於它的manipulate the appearance of that object and also to set/get the value。在編輯框的情況下,String屬性包含框中實際鍵入的內容。因此,我懷疑你實際上想傳遞給tfhdenhnum uicontrols的的處理自己。所以你必須自己提取值並將它們轉換爲數字(str2double)。

hden_value = str2double(get(hden, 'String')); 
hnum_value = str2double(get(hnum, 'String')); 

然後,您可以通過這些tf

sys = tf(hnum_value, hden_value); 

現在應該工作。但是,我相信你真正想要的是當用戶點擊「繪圖」按鈕時從這些編輯框中檢索值。您目前擁有它的方式,只有一次(當GUI啓動時)檢索值,因爲它們位於回調函數之外。如果你想讓他們每次獲取用戶提供的值單擊「plot」按鈕,那麼你需要將上面的代碼放在你的按鈕回調(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)提醒他們選擇了無效值的用戶。