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.
Final GUI for this tutorial:
Initializing GUIDE (GUI Creator)
1. Open up MATLAB. Go to the command window and type in guide
2. Choose the first option Blank GUI (Default)
3. You should now see the following screen.
4. For the adder GUI, we will need the following components.
Add two Axes components to the GUI figure.
Add three Pushbutton components onto the GUI figure.
5. Double click the Axes component to bring up the Property Inspector.
The Tag property is named axes1.
The other Axes component’s Tag property is named axes2.
6. 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 to plotAxes1_pushbutton
7. Double click on the next pushbutton and change the String property to Plot Axes 2 and change the Tag property to plotAxes2_pushbutton.
Double click on the final pushbutton and change the String property to Clear Axes and change the Tag property to clearAxes_pushbutton.
8. The figure should look like below after you add the components and modify them.
9. Save your GUI wherever you please with your desired filename.
Writing the Code for the GUI Callbacks
10. Open up the .m file that was automatically generated when you saved your GUI.
11. In the MATLAB editor, click on the icon (f), which will bring up a list of the functions within the .m file.
Select plot1_pushbutton_Callback.
12. Add the following code to the function:
Copy and paste the code
axes(handles.axes1)
x = 0:10;
y = 0:10;
plot(x,y);
title(‘Axes 1’);
xlabel(‘X data’);
ylabel(‘Y data’);
guidata(hObject, handles);
13. Put the following code into the plot2_pushbutton_Callback:
Copy and paste the code
axes(handles.axes2)
x = 0:5;
y = 0:5;
plot(x,y);
title(‘Axes 2’);
xlabel(‘X data’);
ylabel(‘Y data’);
guidata(hObject, handles);
14. Add some code to the clearPlots_pushbutton_Callback:
Copy and paste the code
cla(handles.axes1,’reset’)
cla(handles.axes2,’reset’)
guidata(hObject, handles);
Add the following line of code to axes_tutorial_OpeningFcn:
Copy and paste the code
set(hObject, ‘toolbar’,’figure’);
This line of code effectively adds the standard toolbar to the GUI, allowing the user to zoom, pan, query the plot, and more.
15. From the GUIDE editor, you can click BUTTON PLAY on the to launch the GUI.
END