繁体   English   中英

如何在Matlab GUI中插入Bode Plot函数

[英]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以了解失败的原因。 您通过了hdenhnum 您以这种方式创建它们。

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

这会将MATLAB 图形对象分配给变量hden 该对象本身可用于操纵该对象的外观并设置/获取其值 对于编辑框, String属性包含在框中实际键入的内容。 因此,我怀疑您实际上想要传递给tfhdenhnum 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并创建波特图。

您可能要向回调添加一些其他错误检查,以确保为hdenhnum输入的值有效,并且将生成有效的图,并可能引发警告( warndlg )或错误( errordlg )以警告用户它们选择的无效值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM