2016-03-08 72 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)提醒他们选择了无效值的用户。