MATLAB®
Creating Graphical User Interfaces
R2015a
How to Contact MathWorks
Latest news:
Sales and services:
User community:
Technical support:
Phone:
www.mathworks.com
www.mathworks.com/sales_and_services
www.mathworks.com/matlabcentral
www.mathworks.com/support/contact_us
508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
MATLAB® Creating Graphical User Interfaces
© COPYRIGHT 2000–2015 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and
govern the use, modification, reproduction, release, performance, display, and disclosure of the Program
and Documentation by the federal government (or other entity acquiring for or through the federal
government) and shall supersede any conflicting contractual terms or conditions. If this License fails
to meet the government's needs or is inconsistent in any respect with federal procurement law, the
government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
November 2000 Online Only
June 2001
Online Only
Online Only
July 2002
Online Only
June 2004
Online Only
October 2004
March 2005
Online Only
September 2005 Online Only
Online Only
March 2006
May 2006
Online Only
September 2006 Online Only
March 2007
Online Only
September 2007 Online Only
Online Only
March 2008
Online Only
October 2008
March 2009
Online Only
September 2009 Online Only
March 2010
Online Only
September 2010 Online Only
April 2011
Online Only
September 2011 Online Only
March 2012
Online Only
September 2012 Online Only
March 2013
Online Only
September 2013 Online Only
Online Only
March 2014
October 2014
Online Only
Online Only
March 2015
New for MATLAB 6.0 (Release 12)
Revised for MATLAB 6.1 (Release 12.1)
Revised for MATLAB 6.6 (Release 13)
Revised for MATLAB 7.0 (Release 14)
Revised for MATLAB 7.0.1 (Release 14SP1)
Revised for MATLAB 7.0.4 (Release 14SP2)
Revised for MATLAB 7.1 (Release 14SP3)
Revised for MATLAB 7.2 (Release 2006a)
Revised for MATLAB 7.2
Revised for MATLAB 7.3 (Release 2006b)
Revised for MATLAB 7.4 (Release 2007a)
Revised for MATLAB 7.5 (Release 2007b)
Revised for MATLAB 7.6 (Release 2008a)
Revised for MATLAB 7.7 (Release 2008b)
Revised for MATLAB 7.8 (Release 2009a)
Revised for MATLAB 7.9 (Release 2009b)
Revised for MATLAB 7.10 (Release 2010a)
Revised for MATLAB 7.11 (Release 2010b)
Revised for MATLAB 7.12 (Release 2011a)
Revised for MATLAB 7.13 (Release 2011b)
Revised for MATLAB 7.14 (Release 2012a)
Revised for MATLAB 8.0 (Release 2012b)
Revised for MATLAB 8.1 (Release 2013a)
Revised for MATLAB 8.2 (Release 2013b)
Revised for MATLAB 8.3 (Release 2014a)
Revised for MATLAB 8.4 (Release 2014b)
Revised for MATLAB 8.5 (Release 2015a)
Contents
Introduction to Creating UIs
About UIs in MATLAB Software
What Is a UI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Does a UI Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ways to Build MATLAB UIs . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-4
1-5
How to Create a UI with GUIDE
1
2
Create a Simple UI Using GUIDE . . . . . . . . . . . . . . . . . .
Open a New UI in the GUIDE Layout Editor . . . . . . . .
Set the Window Size in GUIDE . . . . . . . . . . . . . . . . . .
Layout the Simple GUIDE UI . . . . . . . . . . . . . . . . . . . .
Code the Behavior of the Simple GUIDE UI . . . . . . . .
Open and Run the Simple GUIDE UI . . . . . . . . . . . . .
Files Generated by GUIDE . . . . . . . . . . . . . . . . . . . . . . .
Code Files and FIG-Files . . . . . . . . . . . . . . . . . . . . . . .
UI Code File Structure . . . . . . . . . . . . . . . . . . . . . . . .
Adding Callback Templates to an Existing UI Code File
About GUIDE-Generated Callbacks . . . . . . . . . . . . . .
2-2
2-2
2-5
2-6
2-16
2-22
2-24
2-24
2-24
2-25
2-26
v
3
A Simple Programmatic UI
Create a Simple UI Programmatically . . . . . . . . . . . . . .
Create a Code File for the Simple Programmatic UI . . .
Create a Figure for the Simple Programmatic UI . . . . .
Add Components to the Simple Programmatic UI . . . . .
Code the Simple Programmatic UI Behavior . . . . . . . . .
Verify Code and Run the Program . . . . . . . . . . . . . . .
3-2
3-3
3-3
3-4
3-6
3-10
Create UIs with GUIDE
4
5
What Is GUIDE?
GUIDE: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . .
UI Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
UI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
GUIDE Tools Summary . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
4-2
4-2
4-3
GUIDE Preferences and Options
GUIDE Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Set Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Confirmation Preferences . . . . . . . . . . . . . . . . . . . . . . .
Backward Compatibility Preference . . . . . . . . . . . . . . .
All Other Preferences . . . . . . . . . . . . . . . . . . . . . . . . . .
GUIDE Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The GUI Options Dialog Box . . . . . . . . . . . . . . . . . . . . .
Resize Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Command-Line Accessibility . . . . . . . . . . . . . . . . . . . . .
5-2
5-2
5-2
5-4
5-4
5-8
5-8
5-9
5-9
vi
Contents
Generate FIG-File and MATLAB File . . . . . . . . . . . . .
Generate FIG-File Only . . . . . . . . . . . . . . . . . . . . . . .
5-10
5-12
6
Lay Out a UI Using GUIDE
GUIDE Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Access the Templates . . . . . . . . . . . . . . . . . . . . . . . . . .
Template Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . .
Set the UI Window Size in GUIDE . . . . . . . . . . . . . . . . .
Prevent Existing Objects from Resizing with the
Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Set the Window Position or Size to an Exact Value . . .
Maximize the Layout Area . . . . . . . . . . . . . . . . . . . . .
GUIDE Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Add Components to the GUIDE Layout Area . . . . . . . .
Place Components . . . . . . . . . . . . . . . . . . . . . . . . . . . .
User Interface Controls . . . . . . . . . . . . . . . . . . . . . . . .
Panels and Button Groups . . . . . . . . . . . . . . . . . . . . .
Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ActiveX Component . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resize GUIDE UI Components . . . . . . . . . . . . . . . . . .
Copy, Paste, and Arrange Components . . . . . . . . . . . . .
Select Components . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copy, Cut, and Clear Components . . . . . . . . . . . . . . . .
Paste and Duplicate Components . . . . . . . . . . . . . . . .
Front-to-Back Positioning . . . . . . . . . . . . . . . . . . . . . .
Locate and Move Components . . . . . . . . . . . . . . . . . . . .
Use Coordinate Readouts . . . . . . . . . . . . . . . . . . . . . .
Drag Components . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Use Arrow Keys to Move Components . . . . . . . . . . . . .
Set the Component's Position Property . . . . . . . . . . . .
6-2
6-2
6-3
6-11
6-11
6-12
6-12
6-13
6-17
6-17
6-23
6-44
6-50
6-54
6-65
6-67
6-70
6-70
6-71
6-71
6-72
6-74
6-74
6-75
6-76
6-76
vii
Align GUIDE UI Components . . . . . . . . . . . . . . . . . . . . .
Align Objects Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Property Inspector . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Grid and Rulers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Guide Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Customize Tabbing Behavior in a GUIDE UI . . . . . . . .
Create Menus for GUIDE UIs . . . . . . . . . . . . . . . . . . . . .
Menus for the Menu Bar . . . . . . . . . . . . . . . . . . . . . . .
Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Toolbars for GUIDE UIs . . . . . . . . . . . . . . . . . .
Toolbar and Tools . . . . . . . . . . . . . . . . . . . . . . . . . . .
Editing Tool Icons . . . . . . . . . . . . . . . . . . . . . . . . . . .
View the GUIDE Object Hierarchy . . . . . . . . . . . . . . .
Design Cross-Platform UIs in GUIDE . . . . . . . . . . . . .
Default System Font . . . . . . . . . . . . . . . . . . . . . . . . .
Standard Background Color . . . . . . . . . . . . . . . . . . .
Cross-Platform Compatible Units . . . . . . . . . . . . . . .
UI Design References . . . . . . . . . . . . . . . . . . . . . . . . . .
6-79
6-79
6-82
6-85
6-86
6-88
6-91
6-91
6-101
6-108
6-108
6-116
6-119
6-120
6-120
6-121
6-121
6-123
7
Save and Run a GUIDE UI
Save a GUIDE UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Save a UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create a Backward Compatible GUIDE Fig-File . . . . . .
Append New Callbacks to an Existing GUIDE Code
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Programmatic Files from GUIDE Files . . . . . . .
Rename GUIDE UIs and Files . . . . . . . . . . . . . . . . . . . . .
7-2
7-2
7-2
7-3
7-4
7-5
viii
Contents