简体   繁体   中英

How to insert Bode Plot function in a Matlab GUI

I'm creating a GUI that plots a Bode Plot from an entering data. 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:

Error using tf (line 279) Invalid syntax for the "tf" command. Type "help tf" for more information.

Error in Simple_Plot (line 29) sys = tf(hnum,hden);

Undefined function or variable "sys".

Error in Simple_Plot/tf_Callback (line 36) bode(sys)

Error while evaluating uicontrol Callback

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. 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.

So let's look at what you're passing to tf to see why it fails. You pass hden and hnum . You create them in this way.

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

This assigns a MATLAB graphics object to the variable 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. 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. So you'll have to fetch the values themselves and convert them to numbers ( str2double ).

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

You can then pass these values to 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. 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 ).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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