Create a Simple UI Programmatically

This example shows how to create a simple UI programmatically,
such as the one shown here.

Subsequent topics guide you through the process of creating
this UI.

If you prefer to view and run the code that created this UI
without creating it, set your current folder to one to which you have
write access. Copy the example code and open it in the Editor by issuing
the following MATLAB® commands:

Note:
This code uses dot notation to set graphics object properties.
Dot notation runs in R2014b and later. If you are using an earlier
release, use the set function
instead. For example, change f.Visible = 'on'; to set(f,'Visible','on').

To run the code, go to the Run section
in the Editor tab. Then click Run .

Create a Code File for the Simple Programmatic UI

Create a function file (as opposed to a script file,
which contains a sequence of MATLAB commands but does not define
functions).

At the MATLAB prompt, type edit.

Type the following statement in the
first line of the Editor.

function simple_gui2

Following the function statement, type
these comments, ending with a blank line. (The comments display at
the command line in response to the help command.)

% SIMPLE_GUI2 Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.
(Leave a blank line here)

At the end of the file, after the
blank line, add an end statement.

Note
You need the end statement to specify the
end of the function because the example uses nested functions. To
learn more, see Nested Functions.

Save the file in your current folder
or at a location that is on your MATLAB path.

Create a Figure for the Simple Programmatic UI

Add the following lines before the end statement
in your file to create a figure and position it on the screen.

% Create and then hide the UI as it is being constructed.
f = figure('Visible','off','Position',[360,500,450,285]);

The call to the figure function uses two
property/value pairs:

The Visible property makes the
window invisible so that the user cannot see the components being
added or initialized.

The window becomes visible when the UI has all its components
and is initialized.

The Position property is a four-element
vector that specifies the location of the UI on the screen and its
size: [distance from left, distance from bottom, width, height]. Default
units are pixels.

Each statement uses a series of uicontrol property/value
pairs to define a push button:

The Style property specifies
that the uicontrol is a push button.

The String property specifies
the label on each push button: Surf, Mesh,
and Contour.

The Position property specifies
the location and size of each push button: [distance from left, distance
from bottom, width, height]. Default units for push buttons are pixels.

Each uicontrol call returns the handle
of the push button created.

Add the pop-up menu and its static
text label by adding these statements to the code file following the
push button definitions. The first statement creates a popup menu.
The second statement creates a text component that serves as a label
for the popup menu.

Run your code by typing simple_gui2 at
the command line. You can select a data set in the pop-up menu and
click the push buttons, but nothing happens. This is because there
is no callback code in the file to service the pop-up menu or the
buttons.

Code the Simple Programmatic UI Behavior

Program the Pop-Up Menu

The pop-up menu enables users to select the data to plot. When
a user selects one of the three data sets in the pop-up menu, MATLAB software
sets the pop-up menu Value property to the index
of the selected string. The pop-up menu callback reads the pop-up
menu Value property to determine which item is
currently displayed and sets current_data accordingly.

Add the following callback to your file following the initialization
code and before the final end statement.

Program the Push Buttons

Each of the three push buttons creates a different type of plot
using the data specified by the current selection in the pop-up menu.
The push button callbacks plot the data in current_data.
They automatically have access to current_data because
they are nested at a lower level.

Add the following callbacks to your file following the pop-up
menu callback and before the final end statement.

Program the Callbacks

When the user selects a data set from the pop-up menu or clicks
one of the push buttons, MATLAB software executes the callback
associated with that particular event. Use each component's Callback property
to specify the name of the callback with which each event is associated.

To the uicontrol statement
that defines the Surf push button,
add the property/value pair

Initialize the Simple Programmatic UI

Initialize the UI, so it is ready for the user when the code
makes the UI visible. Make the UI behave properly when it is resized
by changing the component and figure units to normalized.
This causes the components to resize when the UI is resized. Normalized
units map the lower-left corner of the figure window to (0,0) and
the upper-right corner to (1.0, 1.0).