Matlab GUI Tutorial - Plotting Data to Axes
31 Oct 2007 Quan Quach 41 comments 17464 views
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.
Pages: 1 2
41 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