MATLAB GUI Tutorial - Plotting Data to Axes
31 Oct 2007 Quan Quach 112 comments 22,781 views
Introduction
In this Matlab GUI tutorial, you will learn how to create and use the Axes component. The Axes component allows you to display graphics, such as graphs and images on your GUI. In this tutorial, we will create two axes on the GUI and plot some simple data onto it. In addition, we will include a reset button to clear the axes and we will also add the standard toolbar to allow the user to zoom, pan, and query the plot.

This tutorial is written for those with little or no experience creating a Matlab GUI (Graphical User Interface). If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!
Create the Visual Aspect of the GUI
-
First, open up Matlab. Go to the command window and type in
guide.
-
You should see the following screen appear. Choose the first option
Blank GUI (Default).
-
Click on
and add two Axes components to the GUI figure. Next, click on
and add three Pushbutton components onto the GUI figure. -
Double click the Axes component to bring up the Property Inspector. Change the Tag property to
axes1, which should already be the default name. Additionally, make sure the other Axes component’s Tag property is namedaxes2.

-
Next, let’s modify the properties of the Pushbutton components. Double click on one of the Pushbutton components. Change the String property to
Plot Axes 1, and the Tag property toplotAxes1_pushbutton, as shown below.

Similarly, double click on the next pushbutton and change the String property to
Plot Axes 2and change the Tag property toplotAxes2_pushbutton.Finally, double click on the final pushbutton and change the String property to
Clear Axesand change the Tag property toclearAxes_pushbutton. -
Here’s what your figure should look like after you add the components and modify them.

-
Save your GUI wherever you please with your desired filename.
Writing the Code for the GUI
Matlab automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don’t have to worry about any of the other function types.
-
Open up the .m file that was automatically generated when you saved your GUI. In the Matlab editor, click on the
icon, which will bring up a list of the functions within the .m file. Select plot1_pushbutton_Callback.

Add the following code to the function:
%selects axes1 as the current axes, so that %Matlab knows where to plot the data axes(handles.axes1) %creates a vector from 0 to 10, [0 1 2 3 . . . 10] x = 0:10; %creates a vector from 0 to 10, [0 1 2 3 . . . 10] y = 0:10; %plots the x and y data plot(x,y); %adds a title, x-axis description, and y-axis description title('Axes 1'); xlabel('X data'); ylabel('Y data'); guidata(hObject, handles); %updates the handles
-
Similarly, we want to put the following code into the plot2_pushbutton_Callback:
%selects axes2 as the current axes, so that %Matlab knows where to plot the data axes(handles.axes2) %creates a vector from 0 to 10, [0 1 2 3 . . . 10] x = 0:10; %creates a vector [0 1 4 9 . . . 100] y = x.^2 %plots the x and y data plot(x,y); %adds a title, x-axis description, and y-axis description title('Axes 2'); xlabel('X data'); ylabel('Y data'); guidata(hObject, handles); %updates the handles
-
Next, we need to add some code to the clearPlots_pushbutton_Callback:
%these two lines of code clears both axes cla(handles.axes1,'reset') cla(handles.axes2,'reset') guidata(hObject, handles); %updates the handles
-
And finally, we need to add the following line of code to axes_tutorial_OpeningFcn:
set(hObject,'toolbar','figure');
This line of code should be placed right before:
guidata(hObject, handles);
This line of code effectively adds the standard toolbar to the GUI, allowing the user to zoom, pan, query the plot, and more. The standard toolbar and a brief description of the icons are shown below:

-
Save your m-file!
Run and Test the GUI
Now that we’ve completed both the visual and code aspects of the GUI, its time to run the GUI to make sure it works.
-
From the m-file editor, you can click on the
icon to save and run the GUI. Alternatively, from the GUIDE editor, you can click on the
to launch the GUI. The following GUI should appear once you click the icon: 
-
Go ahead and try pressing all of the buttons to make sure they work. If everything was done correctly, you should see the following plots. Also, you can use the icons that are within the red box to test out the other functions.
-
And that’s it. Those are the basics of using the Axes component. You can explore the other options that the axes has to offer through the Property Inspector.
This is the end of the tutorial.
Source files can be downloaded here.
112 Responses to “MATLAB GUI Tutorial - Plotting Data to Axes”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


When i close MATLAB and start guide again my axes loses its TAG and hence becomes invisible.How do i solve this?
nice one! U need to write some more stuff for beginners
Thank you so much ….. I love your style and find your instructions very helpful.
Going through the programs you have written for us, the beginners, I feel more confident of writing my own codes ………..
Thank you once again ……..
What a great tutorial.
Im working on my final project, so this tutorial help me solve my problem writing the code for axes.
Thanks.
how to plot a resultant vector in GUI ex.10N+5N
very nice tutorial..however i had a query for you..
when i do a zoom on the image..and click one pushbutton to make an action..the image return to its initial dimensions(i loose the zoom)…could you help me on this subject?
Hi Nola
good question… although I haven’t tried to code it yet, what you need to do is to get the new axes info and store it somewhere.
For example, your axes is called handles.axes1. You will have to use the get() and get the min,max axis on handles.axes1 and store in a variable for example myaxis.
Now in your pushbutton function… when you have a plot command, make sure you use the axis info in myaxis
How do we plot a graph where the function of graph is key in by user?
I mean we can plot many different function of graph just by changing function?
very nice, it helped me a lot. Thanks, I love the way u have explained the whole process.. very clear!
Muy buena tu pagina, la estare consultando a menudo. De antemano te doy las gracias por la futuras visitas a tu tutorial. Alfredo Huerta
Thank you very much !
I learna lot of skills from this websit!
Dear Sir,
This tutorial helps me a lot. Thanks for it. Will you provide a tutorial on basic concept of the GUIDE ?
With thanks
Abhijit Betal
I used a series of checkboxes, each to plot the derivatives of a function.
When I click on the checkbox, the plot sholud appear and when I unclick it, the plot should disappear. I used ‘hold on’ on each checkbox callboxes, so that when I click all of them I should see all the plots. I tried using cla, but it clears the whole axes.
If I uncheck the first checkbox, only the plot corresponding to it should disappear.
Any help?
can i ask question
i have plot between eficiency and threshold and i get max efficiency
can i get threshold on max efficiency in gui of matlab code
Dear sir,
I want to know how to capture the coordination of my graph?
can you help me with that
Arash,
Can you elaborate on your question? I don’t quite understand it.
Quan
Quan,
I want know is it any way to capture (x,y) of graph after plotting it,
I mean after plotting a graph, I want to be able to click on graph and see dot’s on graph at my every (x,y), for exmple if I am plotting x=y, after plotting I want to be able to clicking on graph and then I get dots on graph at (1,1) (2,2)(-1,-1) and ….
I am not sure if there be anyawy to doing this?(I am really newcomber in Matlab GUI)
I appreciate your help.
Thank you,
Arash
Hi Arash,
Use the data cursor icon to do this. See the last picture in this tutorial. The icon inside the right of the red box is the one you want to use. Click on this and then click on the curve.
Quan
Hi Quan,
Thank you, I didn’t get chance to try yet, I will let you know.
appreciate your help.
Thank you,
Arash
Hi,
Thank you, it really helped, but is it any way to get coordination with out using tool bar, I mean is it any way to write function to do that, having more control on tool bar, for instance if we have to plot, so we have control some how to check coordination of either one at a time and cursar be regular cursar for other one?
Thank you,
Arash
Hi,
to give more, I am really new in Matlab but when I was reading tutorial for Radio Buttons, Toggle Buttons, and Button Group Panel I thought may be I can with
set(handles.fontSelect_buttongroup,’SelectionChangeFcn’,@fontSelect_buttongroup_SelectionChangeFcn);
Create a function for getting coordination and then put push button for my plot so when I click that button or checkbox my cursar goes to getting coordination mode and get that instead of using toolbar.
and I can put 2 different checkbox or button for 2 different plot in my program .
Thank you,
Arash
Arash, a way to get the cursor position in your program without using the toolbar is as follows.
l=datacursormode;
lData=get(l,’DataCursors’);
pos=get(lData,’Position’);
you can also set the cursor position by doing the above in reverse with ’set’ commands. Test it out and you’ll see that it works pretty well.
Paul
Or even
l=datacursormode;
pos=get(l.DataCursors,’Position’);
Hi paul,
I creat 2 button for my 2 figure and put the code you gave me at call_back event of those but I get error.
basically my goal is to control cursor some how when I click the button for that plot when I move my mouse on figure I get coordination of that point, just like a time I use toolbar.
Arash
i’m not sure exactly at what you are trying to accomplish. Here is a little go through that you can just copy and paste into matlabs command prompt that will show the capabilities that I am discussing.
x=[0:.1:10];
y=x^2+2*x;
graph=plot(x,y);
l=datacursormode(graph);
NOW goto the figure that just popped up and put a cursor position on the graph.
NOW go back to the command prompt and type
pos=get(l.DataCursors,’Position’)
you will see that the variable pos holds the coordinates of the cursor.
I do believe that this is the capability that you requested. This should be enough information to help you do whatever you want to do w/ captureing the coordinates of a cursor.
Helped me a LOT with GUI programming. you guys should come out with a book !!
Thank you very much for this tutorial!!!
Matlab’s help files are very confusing for me every time.
These tutorials are very helpful.
Hi Quan Quach, do you have a tutorial about writing a GUI that allows signals, lets say, from a signal generator through serial port, to be displayed on the gui?
Thanks
Hi Roman,
I haven’t dealt with data acquisition using MATLAB. So cannot help you there. Hopefully I will use it in the future and I can write a tutorial on it!
Quan
Hello Quan,
What a nice work this site is.I am an M-beginner.
I have a doubt.
I have a GUIDE application. Now I wanted to uncheck and check the checkboxes by clicking a pushbutton. How can it be done , plz help me.
THANKS IN ADVANCE
Devanand,
In the pushbutton callback, use
set(handles.checkbox,’Value’,1)
to check the checkbox
and set(handles.checkbox,’Value’,0)
to uncheck it.
Devanand,
Sathya seems to hae beaten me to the punch. Thanks Sathya!
Quan
Thanks to you Quan.
For the past six months I have been working on MATLAB and your tutorials have helped me a lot.
Glad you found our site helpful!
Quan
Fantastic tutorial. I was amazed that I could get a small project working right off the bat based on this. However, I have 2 questions:
1. Suppose I have two sliders and want to plot a point on the axes based on the values of the sliders. Which callback function should do the plotting? What’s the best way to approach this?
2. Is there any way to turn off the automatic resizing of the axes? I want the axes to stay fixed regardless of the data being plotted.
Thanks very much!
Liam
Hi Liam,
Glad you found these tutorials helpful.
for question 2:
If you know what your x and y limits will be, you can do the following:
For question 1:
You would want to put your plotting code in the callback for the slider widget. Every time the slider value is changed, the callback is activated. Note that the slider callback is only activated each time you click the mouse.
Good luck!
Thanks, Quan.
Ideally, I’d like the axes to update if any of the sliders are modified. Right now it only updates if the slider within which the plot code is written is clicked on. Is there some sort of refresh plot command?
One more question, how do you retrieve the layout window of a GUI after closing it? If I try reopening the .fig file, the GUI opens in a mode in which it can’t be modified. Thanks very much!
Liam
Hi Liam,
I would write a separate plot function, and then call this plot function within both of your slider callbacks.
For your second question, you want to open it using GUIDE.
At the MATLAB command prompt, type in ‘guide’, and then open an existing project.
Quan
Quan, et al,
I have 3 separate graphs I am trying to display in my GUI window with the use of radio buttons. I have generated and axes box called Graph and initialized it with the a jpeg
When the GUI appears the user enters data that is read in to do some calculations. I have written stand alone code that plots all three graphs (errorbar, bar, X&Y). My main issue I am having is how to separately save those plots or get them displayed when selecting a radio button. With stand alone code I can get all three plots saved as jpegs, but is there a way to just have them displayed directly into axes named Graph. I have some how accidentally done it with the first plot and it looks much cleaner than loading a jpeg (which is what I am doing now). Here is an example of my stand alone plot code. By the way I am using switch and case to interface with the radio puttons. Atlas is my gui code created by guide and guiengine is my function where most of my actual code is located and passed into atlas
Hopefully this isn’t too much to take in. If there is a better way to utilize the Radio Buttons to execute the code I am all for it. I would rather not have a jpeg displayed since its gets severely distorted. Thanks in advance.
Jesse
Well I was wondering why there wasn’t more to the tutorial and then after I sent my message I noticed there was a second page (oops). That got me to accomplish what I wanted above, but I am getting this error in the command window:
Thanks
Jesse
Hi Jesse,
So lets say you have 3 radio buttons. I’m going to assume you know how to use radio buttons, but if not, take a look here (go to page 3 of this tutorial): http://www.blinkdagger.com/matlab/matlab-gui-tutorial-buttons-button-group
First, set up your buttons into the button panel accordingly.
Next, put this code into your button group call back
Hope that helps,
Quan
You r the man….
This helped a lot..
thanks
-Base
Hi,
I would like to have a dynamic plotting: try to update the plot every time I have a new value. The easier way to do it is to re-plot every time all the previous values, but it is too slow, so I am looking for a solution that allows to add only the last value without loosing the previous ones. I cannot use “hold on” because I need a continuous line.
Thanks for your help.
Pietro
First of all thanks for your clear tutorial!
In the comments you write that one can also write a ‘plot function’
I tried to do this, but can’t get it to work…. could you please provide an example of this?
Thanks a lot!
Bernard
Pietro,
How about plotting the last two values instead of the last value? This will still give you a line.
For instance you could do
Hi Bernard,
A simple plot function might look like this:
Thanks Quan, good idea.
Great tutorials.
Pietro
please help meeee!! can someone gv me the solution? i’m creating simple gui with welcoming text and a picture on the top of it..i’ve put the axes..when i generate, the image will be displayed, when i open back, the image gone jez left the axes itself. the same thing happen when i ‘link’ or switch to the next interface..the image gone…pleaseee help me…im using matlab v.7.2
intan_tassya@yahoo.com
function varargout = Welcome_To_GP_DSS(varargin)
% WELCOME_TO_GP_DSS M-file for Welcome_To_GP_DSS.fig
% WELCOME_TO_GP_DSS, by itself, creates a new WELCOME_TO_GP_DSS or raises the existing
% singleton*.
%
% H = WELCOME_TO_GP_DSS returns the handle to a new WELCOME_TO_GP_DSS or the handle to
% the existing singleton*.
%
% WELCOME_TO_GP_DSS(’CALLBACK’,hObject,eventData,handles,…) calls the local
% function named CALLBACK in WELCOME_TO_GP_DSS.M with the given input arguments.
%
% WELCOME_TO_GP_DSS(’Property’,'Value’,…) creates a new WELCOME_TO_GP_DSS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Welcome_To_GP_DSS_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Welcome_To_GP_DSS_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE’s Tools menu. Choose “GUI allows only one
% instance to run (singleton)”.
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Welcome_To_GP_DSS
% Last Modified by GUIDE v2.5 24-Sep-2008 16:22:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(’gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @Welcome_To_GP_DSS_OpeningFcn, …
‘gui_OutputFcn’, @Welcome_To_GP_DSS_OutputFcn, …
‘gui_LayoutFcn’, [] , …
‘gui_Callback’, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% — Executes just before Welcome_To_GP_DSS is made visible.
function Welcome_To_GP_DSS_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Welcome_To_GP_DSS (see VARARGIN)
axes(handles.pic1);
imshow(’1st.jpeg’);
display(’1st.jpg’)
%is this the correct way?when i click ‘next’, the image wont displayed either, when i generate using guide, the image will be displayed. why?
% Choose default command line output for Welcome_To_GP_DSS
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Welcome_To_GP_DSS wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% — Outputs from this function are returned to the command line.
function varargout = Welcome_To_GP_DSS_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% — Executes on button press in NEXT_button1.
function NEXT_button1_Callback(hObject, eventdata, handles)
open GP_DSS.fig
close Welcome_To_GP_DSS;
% is this the correct way to link between the interface?
% hObject handle to NEXT_button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Dear sir
All the tutorials are very helpfull to me thank
you sir
Dear sir
At the moment Im an IT Undergraduate so this is my final year
We have to do a Research so our group member find topics such as face recorgonization system,character recorgonization system
But our lecture pannel say those common topics
so could provide some topics for me
Very nice tutorial
I am doing a GUI for a presentation ate university and i have to plot 5 different images. I put at the .fig the 5 axis, but i would like to know how can i plot a image at an specific axes. Eg. I want to plot the figure car.jpg at axes1 and plate.jpg at axes5. I tried imshow but the images were plot only at the last axes ( axes5).
Thanks
Victor,
use
Quan
Thanks Quan
i AHve another question . I have 5 axes to show 5 different images, but 3 of them depends on parameters that i choose at pop up menu to be shown. So how can i clear the image at the axis when i change to a parameter that i don´t need those 3 images.
Does exist a command like clear(handles.axes1)?
See ya!!
Victor,
cla
is used to clear the current axes.
Hi,
Thanx for the tutorials & the webpage, it’s really clear & this isn’t the only tutorial that i’ve read and develop.
But, here my question.
i’m doing a GUI that letme plot one frame of a 3d object in a fig. out from the GUI, but now i want to plot another frame of the object in the same fig. but that frame gets plotted into the GUI and not in the fig.. i was trying with gca, gco, gcba, & gcbo but this
doesn’t work or i’m not applying fine this orders.
here’s the code of the plot pushbutton, her i charge three matrices of the same size called x y z
checkbox2 is for the plotting of the opposite “frame”
edit1 is where the person type the number of the frame
the number of frames are the number of files
Thanx for the attention.
Herman,
PS.
If u want i can sent you the entire GUI
Hi, first thank you for this golden tutorial…
I’ve a little problem…
I don’t understand why my first plot is in axes1 but the second one isn’t on the gui in axes 2 but appear in a windows like a normal matlab file.m.
please help me…
My .m file isn’t linked to the tutorial but the argument is the same:
% — Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = get(handles.edit1,’String’);
b = get(handles.edit2,’String’);
% a and b are variables of Strings type, and need to be converted
% to variables of Number type before they can be added together
total = 1/((1-(str2num(b))^2)^2+(2*(str2num(a))*(str2num(b)))^2)^(1/2);
c = num2str(total);
% need to convert the answer back into String type to display it
set(handles.text3,’String’,c);
guidata(hObject, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% — Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%selects axes1 as the current axes, so that
%Matlab knows where to plot the data
axes(handles.axes1)
z=0:0.25:1;
for j=1:length(z);
for i = 1: 201
r(i) = (i-1)/100;
d1=((1-r(i)^2)^2+(2*z(1,j)*r(i))^2)^(1/2);
if d1~=0
f(i,j)=1/((1-r(i)^2)^2+(2*z(1,j)*r(i))^2)^(1/2);
else
end
end
end
plot(r,f)
grid
axis([0,1,0,5])
xlabel(’r')
ylabel(’f(r)’)
title(’Andamento della funzione di trasferimento’)
legend(’z=0′,’z=0.25′,’z=0.5′,’z=0.75′,’z=1′)
guidata(hObject, handles); %updates the handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% — Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%these two lines of code clears both axes
cla(handles.axes1,’reset’)
cla(handles.axes2,’reset’)
guidata(hObject, handles); %updates the handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% — Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%selects axes2 as the current axes, so that
%Matlab knows where to plot the data
axes(handles.axes2)
z=[0,0.5,0.6,0.65,0.7,0.75,1];
for j=1:length(z);
for i = 1: 100
r(i) = (i-1)/100;
d2=((1-r(i)^2)^2+(2*z(1,j)*r(i))^2)^(1/2);
if d2~=0
f(i,j)=1/((1-r(i)^2)^2+(2*z(1,j)*r(i))^2)^(1/2);
else
end
end
end
figure
plot(r,f)
grid
axis([0 1 0.94 1.06])
title(’Andamento della funzione di trasferimento’)
xlabel(’r')
ylabel(’f(r)’)
legend(’z=0′,’z=0.5′,’z=0.6′,’z=0.65′,’z=0.7′,’z=0.75′,’z=1′)
guidata(hObject, handles); %updates the handles
the problem is linked to the last plot, the fisrt work perfectly!
thank you friends!
Paolo
Ops i’ve understand the problem…..
the problem was “figure”…
This is great!
Thank you for the amazing tutorial, it is a great help for someone as new to this as me.
However, I had the following issue when I try to run your code on my computer (I just downloaded your axes.zip ):
whenever I click a button I got the error:
??? Attempt to reference field of non-structure array.
Error in ==> axes_tutorial>plot1_pushbutton_Callback at 81
axes(handles.axes1)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> axes_tutorial at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback
is this related to my matlab version? (I am using R2008b) thanks ahed!
Ah nevermind: I was trying to run the *.fig, instead of *.m file, silly me..
Hi,
thanks for this tutorial it helps me a lot. Now I have this issue, I load x and y coordinates to be plotted in the axes object. Following this tutorial it is quite easy, I use the load command to read the data from, for example, x_coor.txt and y_coor.txt assigning them to the variables x and y. Finally I plot the first column of the x and y vector. But what I need to do if I want to update the plot with for example the value of the second column of the two vector. I’d like to avoid to re-load each time the two files because they are huge files with several column and it requires some minutes to be loaded? How is it possible to store the two matrices (x and y) in to the workspace and then access them each time i press the bottom to update the graph with the next column?
I hope it is clear, thanks bye
Hi Barney,
What I think you’re looking for is a command to access a certain column of your matrix. It sounds like you know how to load the data for your x,y coordinates. Hopefully you can load the whole file into some variables X_total, Y_total (or whatever you want to call them)
Then to access the specific column, use this sort of command:
Hopefully that’s what you’re looking for.
-Zane
Thanks a lot for all the tuto
Hi Zane,
actually my problem was to store the data in a variable that can be manipulated each time a press a button or so on, so basically is a problem of variable visibility…
I’ve solved the problem using assignin and evalin command to store the data in the ‘base’ workspace’. May be there is a more elegant solution but that is what I find out.
Thanks Zane
Bye
Hi guys,
another question, is it possible to introduce a knob in a matlab GUI? I asked this question because in the layout editor there is not any command like this one.
Please let me kow if it is possible
Thanks
Hi,
Went through your tutorials.. They helped me a lot.
Refering to your example of plotting data to the axes. What if we define the numerator and the denominator in the form of a matrix and plot using the function bode(den,num) instead 0f plot(x,y).
My requirement is to plot the transfer function obtained from tf(num,den) format.
By using the above plot function bode(num,den) i dont get any display in my gui axes. Please help me to sort this problem.
hashan,
Did you set the axes?
Quan
Hi,
Can you please guide me to set the axes. Or can you place a link which i can learn how to set the axes.
thx 4 responding.
hashan.
Hashan,
See step 1, in the section called: Writing the Code for the GUI.
Quan
@ barney:
Don’t believe there is a knob, closest thing to it is the slider. I guess there are usability issues when you have a knob that you have to rotate. Definitely doable and code-able
hi,
I have a problem that I want to draw parallel coordinate plots using MATLAB. I am using MATLAB 7.0. I want to set different scale for yaxis. so that DATA should be understandable. Can you help me in that regard.
Hi, I did all the steps but when I press the function icon on the tool bar it doesnt bring the callback functions. I checked the tags like 3 times. Does anyone have an idea?
hi
tnx alot for all the guide! its so useful
but i have the same problem as hashan. i think GUI cant show the bode diagram in axes.
is there any way,other than using plot for the bode?
@ Anil
It sounds like you either want to use plotyy or subplot features.
type ‘doc plotyy’ or ‘doc subplot’ in the command window to see how they’re used.
@ mitra
I don’t have bode feature on this computer, but I’ve used it before. Hopefully just plotting (20*log10(abs(TF)) isn’t too bad. But you could try playing around with gcf/get/set commands after making the bode plot to reconstruct your own plot from the bode diagram (if it has manipulatable axes)
-Zane
Quan,
Very nice tutorial. I have a similar gui, except that one of the plots is a 3D plot. When I try to zoom in on that 3D plot it grows until it fills the entire figure. This behavior is undesireable. I would not like for anything outside of the axes boundary to be displayed. I tried several ways to fix this with no success. I tried making sure that the ‘clipping’ property for the figure was set to ‘on’. I also tried putting this axes in uipanel whose ‘clipping’ property was set to ‘on’.
Do you have a solution for this problem?
Thanks in advance for any help you can provide.
-Seth
sir,i want to display various signal on gui using matlab.
To Hashan & Mitra,
Change Tools->GUI Options->Resize Behavior to Proprotional
Try again.
Dear Rick
It worked!!!I dont know how to thank you,you just made my day !!
Mitra
Thank you very much for the tutorial, it’s really good.
Hello
I am trying to make a plot and I want my y-value to be the string value coming from a different static text box. I tried using
y=get(handles.result_editText,’String’);
but that does not work. What is the code for this?
Thanks!!!!
Christina
Christina,
You won’t be able to plot ’string’ values, so you need to convert them to ‘double’ format first.
-Zane
Hi, may i ask u a question about plotting on axes. I need to display my chromosome ideogram on a axes of GUI. I have tried the way u described above, but it doesnt work. THe result is still a blank axes, the ideogram were not docked in the axes. Followings are my codes :
function pushbutton1_Callback(hObject, eventdata, handles) sample1=struct('Chromosome',[1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 ... 8 8 8 8 8 8 8 8 9 9 9 9 9 9 10 10 10 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 13 13 13 14 14 15 15 15 15 15 16 16 16 16 16 16 ... 17 17 17 17 17 17 17 17 18 18 18 18 18 18 18 19 19 19 20 20 20 21 21 22 22 22 23 23 23 23],'CNVType',[2 2 1 1 2 1 2 2 2 2 1 2 ... 2 1 2 2 2 ... 2 2 1 2 2 2 2 2 2 2 2 ... 1 1 2 2 2 2 2 2 2 ... 1 1 1 2 1 ... 2 2 2 2 2 1 1 1 2 1 ... 2 2 2 2 1 ... 1 2 1 2 2 1 1 1 ... 1 2 1 1 1 1 ... 1 2 1 ... 1 1 1 1 1 2 1 1 ... 1 1 1 1 1 1 1 ... 1 1 1 ... 2 2 ... 2 2 2 2 2 ... 2 1 1 1 1 1 ... 2 2 1 2 1 2 1 1 ... 1 1 1 1 1 1 1 ... 2 2 2 ... 2 1 2 ... 1 1 ... 2 2 2 ... 2 2 2 2],'Start',... [S.SegmentData(1).Start; S.SegmentData(2).Start; S.SegmentData(3).Start; S.SegmentData(4).Start; S.SegmentData(5).Start; S.SegmentData(6).Start; ... S.SegmentData(7).Start; S.SegmentData(8).Start; S.SegmentData(9).Start; S.SegmentData(10).Start; S.SegmentData(11).Start; S.SegmentData(12).Start; ... S.SegmentData(13).Start; S.SegmentData(14).Start; S.SegmentData(15).Start; S.SegmentData(16).Start; S.SegmentData(17).Start; S.SegmentData(18).Start; ... S.SegmentData(19).Start; S.SegmentData(20).Start; S.SegmentData(21).Start; S.SegmentData(22).Start; S.SegmentData(23).Start],'End',... [S.SegmentData(1).End; S.SegmentData(2).End; S.SegmentData(3).End; S.SegmentData(4).End; S.SegmentData(5).End; S.SegmentData(6).End; S.SegmentData(7).End; ... S.SegmentData(8).End; S.SegmentData(9).End; S.SegmentData(10).End; S.SegmentData(11).End; S.SegmentData(12).End; S.SegmentData(13).End;S.SegmentData(14).End; ... S.SegmentData(15).End; S.SegmentData(16).End; S.SegmentData(17).End; S.SegmentData(18).End; S.SegmentData(19).End; S.SegmentData(20).End; ... S.SegmentData(21).End; S.SegmentData(22).End; S.SegmentData(23).End]); axes(handles.axes1); chromosomeplot('hs_cytoBand.txt','cnv',sample1,'unit',2); guidata(hObject, handles);Can u help me troubleshoot this codes. Thanks!!!
Roy, since you’re not using one of the MATLAB default plotting commands, I don’t think we’ll be able to help you. It looks like your ‘chromosomeplot’ function is what you’re using to plot. You may want to look into having chromosomeplot output plot data into your GUI e.g.
[x1,x2,...,y1,y2]=chromosomeplot(’hs_cytoBand.txt’,'cnv’,sample1,’unit’,2);
And then plot it manually in the GUI following the example in this tutorial.
You will need to know how the code works to use those outputs and may need to setup the outputs if it doesn’t have them yet.
Good luck!
Hello Guys,
Anyone knows how to add a readme.txt to a a customized tool bar. I created a toolbar button called “About” and a sub link called “Readme”. What I wish to do is to have a .txt file linked to the readme tab…thanx
You are my personal hero!
Thanks for the previous help.But i still can’t work out the solution. It’s ok , I’m confusing about one question now, which is about GUI in matlab. I need to creat some subpanels in a main panel. I step up the subpanels. When I program the GUI, I set the ‘Visible’ property of fist subpanel to ‘off’, it will result in that I cant acess the rest of subpanels. what is the solution for containing some subpanels with stepping up one another in a main panel. Thanks.
thanks for these great tutorials Quan Quach.
Ive been looking through the GUI tutorials but i seem to be stuck at some code.
I have a ‘normal’ m-file (not GUI) and that works fine, I am trying to put this script in a GUI, but i am getting lots of errors and I cant figure it out.
The working m file script
The GUI script
The error codes
Thanks in advance
Eastmus,
You are multiplying two ‘Strings’ by each other when you use the ‘get’ command and then do: RC=R*C;
So you’re basically multiplying ‘LEET’ * ‘ROAR’ when you should be multiplying 1337*2042 (words vs numbers)
I predict that you’ll get a few errors because of this, but it can be easily solved.
As far as finding errors in the future, notice that that was the section of code that changed from your m-file to GUI, so it is most likely the culprit for your errors and you can focus debugging that section. (you can also post just the function your having issues with instead of the whole GUI ^_^)
thanks a lot Zane.
I totally forgot about str2double,
i read this tutorial and the MyAdder and combined both.
these new stuff are still fresh for me and its easy for me to look over them
thanks again, its working
Hi,
I am doing this GUI which plots graphs. I am able to have the plot on the main figure with all the inputs. However I would like to do so that my GUI only takes in the inputs and when i press “Plot” the figure pops up in a new window (like what you get when you do the plot function on Matlab). Any ideas?
Another question is this.
My GUI actually plots the velocity vectors and the trajectory of 2 differential equations. I can get these two plots on the same axes. However I would like to do so that when the user clicks on the plotted graph of velocity vectors, it plots the trajectory i.e. getting information (initial conditions so that i can plot my trajectory) from a click on the graph.
Hope you can help me. Thanks for the awesome tutorials.
[...] GUI Tutorial - Slider MATLAB GUI Tutorial - Pop-up Menu MATLAB GUI Tutorial - Plotting Data to Axes MATLAB GUI Tutorial - Button Types and Button Group MATLAB GUI Tutorial - A Brief Introduction to [...]
hi
i am trying to figure out how to load a graph into a axis , like the one you made in your example.. im not sure how to load it from file, then put it into the axes.. thanks
thanks for the tutorial
Thank you.
Very well written .
Hi,
I need to use “pzmap” function and plot the data in axes1.
Can you help me?
thanks
Hi,
I was trying to draw some simple wire-frame polyhedrons with 3d plots and lines but it doesn’t show up properly. I drew a tetrahedron with a different color for each edge. Problem is, lines/edges that are suppose to be behind others appear in front of them.
Is this normal? Is MATLAB smart enough to determine which lines are suppose to be in front/back? Or am I not doing this right? Help would be very much appreciated.
Thanks!
I AM HAPPY,IS WHAT I WOULD LIKE TO SAY
U CONTINUE IS WHAT I ADD
I APPRECIATE .. WHAT U’R COBTRRIBUTING TO BEGINNERS.
Hello Quan and Daniel
Thanks for creating the best matlab tutorial site.
I came across the following .m file on the File Exchange called GTrace.m which shows the cursor positions on any plots. Pretty nice tool to pimp up a GUI.
Start of file GTrace.m
End of file.
I tried using it with plotyy in a matlab gui (which I learned to do from your site)but can’t seem to get it to work. I am sure you will know exactly how to make it work. Have you written a similar tool before that you can share? Thanks for posting all the great matlab tips - Ray
awesome!!
easy , quick, helpful
I am working on face recognition project,
I have the source code ready,now i want to create the interface.
I don’t know how to load images into the interface
I have many figure windows after the code executes,how do i connect them to the interface.
Please suggest
Thank u
Thanks a lot! Your tutorials made me understand GUI’s so now I can make my own!
Thanks for this guide !!! It’s Great !!! ^_^
” set(hObject,’toolbar’,'figure’); ” is a highly undesirable command and caused a lot of troubles for my perfectly stable and running GUI. Wish I hadn’t followed this tutor’s advice.
Very useful - almost too easy!
Thanks for this tutorial. I would like to ask a question about colorbar. I want to add a colorbar near my axis component. But I didn’t. If you help, I would be appreciated.
Thanks a lot.This helped me on something I have trying to figure out for a long time.
Hi I’m trying to plot the movement of my robot on my GUI but its not working.
what I’ve tried is using a while loop with plot(x,y) and hold on.
can you give me some tips.
the plotting of my code is here:
Ohh men. This all, are very usefull tutorials.
I’ m having a question too. I try to put the data from a pcolor comand to axes in a gui i built, for my final project, for example when i click on a push button. How i ‘ m going to
import the pcolor comand into a push button ?
Thanks in advance for any help you can give me.
Louk
Hi, first thank you for this golden tutorial…
I have an axes to plot a graphic but without code only with mouse movement on axes area in GUI. My aim is calculating the fourier coefficients belonging to grafic which is plotted by user. Please help me. Thanks for everyone…
Hello,
I am very new to Matlab and I found this tutorial to develop GUIs and I think it is very very helpful. Though all the other examples have worked for me but this plot example does not function properly. I have included everything as per the directions given in this tutorial but I error pushing any of the push buttons. The error is
axes2.m and axes2.fig are my file names. (Do not confuse)
———————————————————————————————————-
??? Attempt to reference field of non-structure array.
Error in ==> axes2>plotAxes1_pushbutton_Callback at 83
axes(handles.axes1)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> axes2 at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)axes2(’plotAxes1_pushbutton_Callback’,hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
———————————————————————————————————–
I have tried other examples also but I usually get this error. When I run the GUI through guide then it works properly but when I run it directly, the above error occurs. I have checked the tutorial 10 times and I dont think I have mande any mistakes. Please help me.
Here is the axes.m file code. Waiting for early reply.
I’m new to matlab GUI.. I found ur tutorial very useful to start with. Thanks.
My requirement is to load a signal which I have acquired from an ADC, I stored it in .mat format.. now I have need to browse for this .mat file and display it in my GUI.. can u help me on this
Hi there,
my name is Jan, and i am new to matlab.
last week i started with the add-gui tutorial, pimped it a little bit the last few days, and now i got a little calculater^;)
now i got a new challenge.
i want to plot functions, wich i can put into a textfield.
also i want to get the roots of this function (i.e. x^3-4*x.8)
the problem is, i can print the function with ezplot (plot would be better), but i cant solve the function.
may someone can help me with this
thx to everyone
this site is awesome!