Exercises App Designer
Exercises App Designer
MATLAB APPLICATION
DESIGNER
EXERCISES
Before starting any design, it is a good idea to make a rough sketch of the what you want
the app to look like.
Edit Field
Function to Plot
Axes
Limits
min max
Grid Colour
The purpose of the app we are building today is to plot the MATLAB expression entered
into the Edit Field at the top of the figure. The graph will be plotted between the two
values in the Edit Fields in the bottom right Panel. The Check Box will toggle the grid on
and off. The Drop Down Menu will be used select the colour of the graph.
The first exercise will produce the minimum required to get the app working. The following
exercises will each add more controls and extra functionality.
1
Exercise 1 (The Basic Program)
In this exercise you will create your app and start laying out the graphical objects and
controls. You will also automatically generate the MATLAB program and edit it to produce
a basic function plotter.
Click on app.UIFigure in the browser, to select the figure, then click in the grey region in
the browser below app.UIFigure to deselect the figure.
The grey rectangle in the centre of the window is the figure. Notice that this has a blue
edge when selected. You can also select the figure by clicking on the rectangle.
2
Setting the Figure Size
In the bottom left and right hand corners of the designer window are arrows that
allow you to collapse and expand the panels on the left and right hand sides. Try
these out now.
Collapse the panels on the right, but leave the Component Library open on the left.
Drag the bottom right hand corner on the figure so that the figure occupies the majority of
the space between Component Library and the right hand side.
3
Placing the Components on the Figure
You should be on the CANVAS tab at the top of the
window. On the VIEW region of the icon bar, enable
Show grid and set the Interval to 25, as shown on
the right.
4
Component Properties
Expand the panel on the right hand side so that you can see the Component Properties
window. Then click on the Edit Field at the top of the figure.
2. The Label text is Function to Plot and the text in the Edit Field box is sin(x).
You may need to adjust the size of the Edit Field again.
In the Component Browser window, double click on the Axes and change the name to
MainUIAxes.
5
The figure should now look like this.
We have finished the layout of the app for now. The next step is to look at the code.
Above the top right of the figure is a box as show on the right.
Click on Code View.
At first sight, the code can look quite intimidating. However, all code in the program so far
is not editable. You cannot change it even if you wanted to. You can create a perfectly
functional app without understanding this code.
The program creates the figure and all the components. You can type things into
the Function to Plot Edit Field, but it does not do anything as yet.
6
Creating a Call Back Function
We want something to happen when we type something into the Edit Field. For that we
need a callback function that will execute after the text in the field has been changed.
7
You should now be able to see the callback function in the code.
end
end
Notice that we have for the first time a section of code that is not greyed. We can edit the
code in the white box in the function. However, before we do that, we are going to create
our own function in the code that we can use in the callback function.
8
Adding Your Own Functions
end
end
Paste the line of code, that you cut out earlier, into the function func.
function results = func(app)
value = app.FunctiontoPlotEditField.Value;
end
This line of code takes the text written in the Edit Field and puts it into the variable value.
This will contain the MATLAB expression that we want to plot.
9
We will assume that the expression typed in will use x as the independent variable.
function replot(app)
% Function to replot the graph
Notice that when you plot in the app, you need to specify which axes the plot is going go
into.
Try entering you own MATLAB expressions into the edit field on the app and see if it plots.
10
The Startup Function
If you restart the app, you will find that the default expression at start time is not plotted.
You can change this by calling replot in the figure startup function.
In the Code Browser on the left, select Callbacks and click on the + Callback button.
Run the app. You should find that sin(x) is plotted straight away.
11
Exercise 2 (Expression Error Recovery)
Enter an erroneous expression into the function to be plotted.
For example, x^2 will not work because a dot is needed after the x.
At the moment the program crashes. What would be better is to detect the error and
report what is going wrong without crashing the program.
with
try
replot(app);
catch err
errordlg(err.message,'Expression Error');
end
Enter the expression again to see the error dialogue. The program should not crash this
time.
12
Exercise 3 (The Grid Check Box)
In this exercise, you will add a Check Box to toggle the grid on and off.
The Check Box properties will be shown in the Component Properties window.
Change the following properties of the Check Box.
13
Editing the Code
We can determine the state of the Check Box by looking at it's value.
• If the Check Box is ticked, the value will be one.
• If the Check Box is not ticked, the value will be zero.
In the Code Browser, select Callbacks and click on the + Callback button.
In the callback function for the Check Box, add the following code.
%Read in the status of the Check Box
Status = app.CheckBox.Value;
14
Exercise 4 ( The Colour Drop Down Menu)
In this exercise you will add a Drop Down menu to change the colour of the line.
Drag a Drop Down onto the figure to the right of the grid Check Box.
R2017a - R2018a
Change the following properties of the Drop
Down:
3. Option1 to Red
4. Option2 to Green
5. Option3 to Blue
R2018b
6. Set Green as the default colour at
start up.
15
Edit the function replot. Replace the plot command:
%Plot the graph
plot(app.MainUIAxes,x,y)
Save and run the app. Select a different colour using the pop-up menu. Don't worry if the
colour does not change straight away. At this stage the colour will only change after a
new expression has been entered. So enter a new expression.
Question 1
The reason that the colour only changes when you enter a new expression is that replot
only runs at the start of the program and in the callback function of the Edit Field. It does
not run when you select a new colour with the Drop Down menu.
What can you do to force the graph to plot when you select a new colour?
The answer is on the next page.
16
Answer to Question 1
You call the function replot in the Drop Down menu callback function.
17
Exercise 5 (X Axis Limits)
In this exercise you are going to add a panel with two Edit Fields that specify the limits of
the X axis.
Drag a Panel onto the figure. Don't worry about the size or position of the Panel yet.
Just drag it to the centre of the figure.
Change the following properties of the Panel.
Drag the label above the Edit Field box and adjust the position
so that you see an orange centre line through both the Label and
the box.
Click on the Edit Field away from the both the Label and the box, so that both are
selected.
18
Change the Text of the new Edit Field to Max.
Click on the Panel and drag the bottom edge of the panel up, to just below the Edit Fields.
If there is not sufficient room for the Panel, resize the MainUIAxes to make extra space
available.
to
% Define the limits of x
minx = app.MinEditField.Value;
maxx = app.MaxEditField.Value;
Add a callback function for each Edit Field to run replot when the value has been
changed.
19
Exercise 6 (Using the App in other MATLAB Programs)
Make sure that the App has been saved, then quit the App Designer.
The app is an object. You can access the app using the same method used to access a
graphical object.
>> myapp = FunctionPlot
>> myapp.MainUIAxes
This means that you can use the app in your own programs.
You should have a MATLAB script called UseApp. Open the script to see what it does and
then run the script.
UseApp.m
%UseApp
%This program shows how you can get your own programs to use the
App.
20
Exercise 7 (Packaging a MATLAB App)
You may have noticed the APPS tab at the top of MATLAB. You can package up your App
so the other users can install the App onto the APPS tab.
Hit the Package button on the right. Wait for it to say Packaging Complete.
Function Plot.prj The project file that contains the information that you have
entered in to the Package App window. Clicking on this file
will reopen the project in the Package App window.
Function Plot.mlappinstall Is the file to install the app into MATLAB. This is the file that
you send to users that want to use the app.
To install the App into MATLAB, double click file Function Plot.mlappinstall.
The installed App will not run while you are in the this folder. Right click in the Current
Folder window and select New Folder. Then double click on the New Folder.
To run the app, click on the down arrow on the right of the Apps banner at the top of
MATLAB.
To remove the app, right click on the app and select Uninstall.
21