简体   繁体   English

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

[英]How to insert Bode Plot function in a Matlab GUI

I'm creating a GUI that plots a Bode Plot from an entering data. 我正在创建一个GUI,该GUI根据输入的数据来绘制波特图。 I have the following code but it's giving me an error that I don't understand. 我有以下代码,但它给了我一个我不明白的错误。

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

There're appearing these errors on IDLE: 在IDLE上出现以下错误:

Error using tf (line 279) Invalid syntax for the "tf" command. 使用tf时出错(第279行)“ tf”命令的语法无效。 Type "help tf" for more information. 键入“ help tf”以获取更多信息。

Error in Simple_Plot (line 29) sys = tf(hnum,hden); Simple_Plot中的错误(第29行)sys = tf(hnum,hden);

Undefined function or variable "sys". 未定义的函数或变量“ sys”。

Error in Simple_Plot/tf_Callback (line 36) bode(sys) Simple_Plot / tf_Callback(第36行)bode(sys)中的错误

Error while evaluating uicontrol Callback 评估uicontrol回调时出错

The errors that you are seeing are due to the fact that your call to tf fails and as a result, sys never gets defined. 您看到的错误是由于您对tf的调用失败而导致的,因此sys从未得到定义。 Then in your callback ( tf_Callback ) you try to use sys but since it was never created it can't find it and you get the second error. 然后在您的回调( tf_Callback )中尝试使用sys但是由于从未创建过sys ,所以找不到它,并且出现了第二个错误。

So let's look at what you're passing to tf to see why it fails. 因此,让我们看一下传递给tf以了解失败的原因。 You pass hden and hnum . 您通过了hdenhnum You create them in this way. 您以这种方式创建它们。

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

This assigns a MATLAB graphics object to the variable hden . 这会将MATLAB 图形对象分配给变量hden This object itself can be used to manipulate the appearance of that object and also to set/get the value of it. 该对象本身可用于操纵该对象的外观并设置/获取其值 In the case of an edit box, the String property contains what is actually typed in the box. 对于编辑框, String属性包含在框中实际键入的内容。 So what I suspect you actually want to pass to tf is the value of the hden and hnum uicontrols and not the handles themselves. 因此,我怀疑您实际上想要传递给tfhdenhnum uicontrols的 ,而不是句柄本身。 So you'll have to fetch the values themselves and convert them to numbers ( str2double ). 因此,您必须自己获取值并将其转换为数字( str2double )。

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

You can then pass these values to tf . 然后,您可以将这些传递给tf

sys = tf(hnum_value, hden_value);

Now that should work. 现在应该可以了。 But, I believe what you really want is to retrieve the values from those edit boxes when the user clicks the "plot" button . 但是,我相信您真正想要的是当用户单击“绘图”按钮时从那些编辑框中检索值。 The way that you currently have it, the values are only retrieved one time (when the GUI launches) since they lie outside of the callback function. 您目前所拥有的方式,由于值位于回调函数之外,因此仅会检索一次(在GUI启动时)。 If you want them to fetch the user supplied values each time the "plot" button is clicked, then you'll want to put the above code within your button callback ( tf_Callback ). 如果你希望他们每一个 “阴谋”按钮被点击的时间来抓取用户提供的值,那么你会希望把你的按钮回调( 上面的代码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

Now every time the user clicks the button, the values will be retrieved from the edit boxes, sys will be computed, and a bode plot will be created. 现在,每次用户单击按钮时,将从编辑框中检索值,将计算sys并创建波特图。

You may want to add some additional error checking to the callback to make sure that the values entered for hden and hnum are valid and will produce a valid plot and maybe throw a warning ( warndlg ) or error ( errordlg ) to alert the user that they selected invalid values. 您可能要向回调添加一些其他错误检查,以确保为hdenhnum输入的值有效,并且将生成有效的图,并可能引发警告( warndlg )或错误( errordlg )以警告用户它们选择的无效值。

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

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