MATLAB Creating Graphical User Interfaces

(ff) #1

NoteThis 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


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


(^1) At the MATLAB prompt, type edit.
(^2) Type the following statement in the first line of the Editor.
function simple_gui2
(^3) 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)
(^4) At the end of the file, after the blank line, add an end statement. This end statement
is required because the example uses nested functions. To learn more, see “Nested
Functions”.
(^5) Save the file in your current folder or at a location that is on your MATLAB path.
Create the Figure Window
To create a container for your app’s user interface (UI), add the following code before the
end statement in your file:
% 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 creates a traditional figure and sets the following
properties:
Create a Simple App Programmatically

Free download pdf