Structure of Programmatic App Code Files

File Organization

Typically, the code file for an app has the following ordered sections. You can help to maintain the structure by adding comments that name the sections when you first create them.

  1. Comments displayed in response to the MATLAB® help command.

  2. Initialization tasks such as data creation and any processing that is needed to construct the components. See Initialize a Programmatic App for more information.

  3. Construction of figure and components.

  4. Initialization tasks that require the components to exist, and output return. See Initialize a Programmatic App for more information.

  5. Callbacks for the components. Callbacks are the routines that execute in response to user-generated events such as mouse clicks and key strokes. See Write Callbacks for Apps Created Programmatically for more information.

  6. Utility functions.

File Template

This is a template you can use to create an app code file:

function varargout = myui(varargin)
% MYUI Brief description of program.
%       Comments displayed at the command line in response 
%       to the help command. 

% (Leave a blank line following the help.)

%  Initialization tasks

%  Construct the components

%  Initialization tasks

%  Callbacks for MYUI

%  Utility functions for MYUI


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

Run the Program

You can display your UI at any time by executing the code file. For example, if your code file is myui.m, type


at the command line. Provide run-time arguments as appropriate. The file must reside on your path or in your current folder.

When you execute the code, a fully functional copy of the UI displays on the screen. If the file includes code to initialize the app and callbacks to service the components, you can manipulate components that it contains.

Related Topics