# Learning to Program with MATLAB

## Building GUI Tools

2. Edition September 2022

288 Pages, Softcover*Textbook*

**978-1-119-90047-4**

Learning to Program with MATLAB

Introductory text integrating science, mathematics, and engineering to give a basic understanding of the fundamentals of computer programming with MATLAB

Learning to Program with MATLAB: Building GUI Tools, Second Edition serves as a compact introduction to computer programming using the MATLAB language, covering elements of both program and graphical user interface (GUI) design to enable readers to create computer programs just like the ones they are accustomed to interacting with. Rather than being encyclopedic in scope, the goal of the text is to describe what users will find most useful and point to other features. Descriptions and examples of some of the most useful functions are included throughout, particularly with regards to engineering and science applications. The work also includes updated videos and problem solutions on an instructor companion website.

The first edition of Learning to Program with MATLAB employed the MATLAB graphical user interface design environment (GUIDE) to develop the GUI tools. The second edition is based on the new and improved App Designer program, which has supplanted GUIDE. This edition includes:

* Core concepts of computer programming using MATLAB, such as arrays, loops, functions, and basic data structures

* How to write your own MATLAB functions, covering topics such as local workspaces, multiple outputs, function files, and other functional forms

* The new string class and table class, some new features of function arguments, and re-written sections for building GUI tools with App Designer

* Syntax for graphics and App Designer features, plus examples demonstrating the new way to handle string information

Starting with the basics and building up to an emphasis on GUI tools, Learning to Program with MATLAB is a comprehensive introduction to programming in a robust and multipurpose language, making it an ideal classroom resource for both students and instructors in related programs of study.

1 Getting Started

1.1 Running the MATLAB IDE

1.1.1 Manipulating windows

1.2 MATLAB variables

1.2.1 Variable assignment statements

1.2.2 Variable names

1.2.3 Variable workspace

1.3 Numbers and functions

1.4 Documentation

1.5 Writing simple MATLAB scripts

1.6 A few words about errors and debugging

1.7 Using the debugger

2 Vectors and Strings

2.1 Vector basics

2.2 Operations on vectors

2.3 Special vector functions

2.3.1 Statistical functions

2.4 Using rand and randi

2.5 String basics

2.6 String operations

2.7 Character vectors

2.8 Getting information from the user

3 Plotting

3.1 The plot command

3.2 Tabulating and plotting a simple function

3.3 Bar graphs and histograms

3.4 Drawing several plots on one graph

3.4.1 Multiple plots with a single plot command

3.4.2 Combining multiple plots with a hold command

3.5 Adding lines and text

3.6 Changing object properties

4 Matrices

4.1 Entering and manipulating matrices

4.2 Operations on matrices

4.3 Solving linear systems: the backslash operator

4.3.1 Extended example: solving circuit problems

4.4 Special matrix functions

5 Control flow commands

5.1 Conditional execution: the if statement

5.2 Logical expressions

5.3 Logical variables

5.4 for loops

5.5 while loops

5.6 Other control flow commands

5.6.1 Switch-case statement

5.6.2 Break statement

6 Animation

6.1 Basic animation

6.2 Animating function plots

6.3 Kinematics of motion

6.3.1 One-dimensional motion: constant speed

6.3.2 Motion with constant acceleration

6.3.3 Time-marching dynamics: non-constant force

7 Writing your own MATLAB functions

7.1 MATLAB function files

7.1.1 Declaring MATLAB functions

7.2 Function inputs and outputs

7.3 Local workspaces

7.4 Multiple outputs

7.5 Function files

7.6 Other functional forms

7.6.1 Subfunctions

7.6.2 Nested functions

7.6.3 Anonymous functions

7.7 Optional arguments for functions

8 More MATLAB Data Classes and Structures

8.1 Cell arrays

8.2 Structures

8.3 Complex numbers

8.4 Function handles

8.5 Tables

8.6 Other data classes and data structures

II Building GUI Tools

9 Building GUI Tools with App Designer

9.1 The App Designer interface

9.2 Getting started: HelloTool

9.3 Components communicating: SliderTool

9.4 Transforming a MATLAB program into a GUI tool: DampedEfieldTool

9.4.1 Step 0: Write and debug the program

9.4.2 Step 1: Plan the GUI

9.4.3 Step 2: Create the GUI in App Designer

9.4.4 Step 3: Connect program inputs and outputs to the GUI components

9.4.5 Step 4: Add callbacks to invoke the primary model function

9.5 Test and improve

10 More GUI Techniques

10.1 Sharing data between callbacks

10.2 More GUI components

10.3 Popups

10.3.1 Progress dialogue

10.3.2 Waitbar

10.3.3 Input dialogue

10.3.4 Confirm dialogue

10.4 Responding to keyboard input

10.5 Mouse events and object dragging

III Advanced Topics

11 More Graphics

11.1 Logarithmic plots

11.2 Plotting functions on two axes

11.3 Plotting surfaces

11.4 Plotting vector fields

11.5 Working with images

11.5.1 Importing and manipulating bit-mapped images

11.5.2 Placing images on surface objects

11.6 Rotating composite objects in three dimensions

12 More Mathematics

12.1 Derivatives

12.1.1 Derivatives of mathematical functions expressed as MATLAB functions

12.1.2 Derivatives of tabulated functions

12.2 Integration

12.2.1 Integrating tabulated functions

12.2.2 Integrating mathematical functions expressed as MATLAB functions

12.3 Zeros of a function of one variable

12.4 Function minimization

12.4.1 Finding a minimum of a function of one variable

12.4.2 Multidimensional minimization

12.4.3 Fitting to an arbitrary function by multidimensional minimization

12.4.4 Solving simultaneous nonlinear equations by multidimensional minimization

12.5 Solving ordinary differential equations

12.6 Eigenvalues and eigenvectors

13 Reading and writing files

13.1 Saving and loading data in .mat files

13.2 Reading and writing spreadsheet files

13.3 Writing text files

13.3.1 The writematrix command

13.3.2 Writing formatted text files

13.3.3 Formatting a string using sprintf

13.4 Reading data from a text file

13.4.1 Reading into a cell array

13.4.2 Reading complicated text data filest

13.5 A GUI interface to file names using uiputfile and uigetfile

Appendix A: Using LATEXcommands