+353-1-416-8900REST OF WORLD
+44-20-3973-8888REST OF WORLD
1-917-300-0470EAST COAST U.S
1-800-526-8630U.S. (TOLL FREE)

Learning to Program with MATLAB. Building GUI Tools. Edition No. 2

  • Book

  • 288 Pages
  • September 2022
  • John Wiley and Sons Ltd
  • ID: 5842445
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.

Table of Contents

Preface to the Second Edition xiii

About the Companion Website xvii

I MATLAB Programming 1

1 Getting Started 3

1.1 Running the MATLAB IDE 3

Manipulating windows 5

1.2 MATLAB variables 5

Variable assignment statements 6

Variable names 7

Variable workspace 8

1.3 Numbers and functions 8

1.4 Documentation 9

1.5 Writing simple MATLAB scripts 10

Block structure 11

Appropriate variable names 11

Useful comments 11

Units 11

Formatting for clarity 12

Basic display command 12

1.6 A few words about errors and debugging 12

Error messages are your friends 13

Sketch a plan on paper first 13

Start small and add slowly 13

1.7 Using the debugger 13

Looking ahead 14

Programming Problems 14

2 Vectors and Strings 19

2.1 Vector basics 20

2.2 Operations on vectors 21

Multiplication by a scalar 21

Addition with a scalar 21

Element-by-element operation with two vectors 21

Functions of vectors 22

Length of vectors 22

Subarrays 23

Concatenating vectors 23

2.3 Special vector functions 23

Statistical Functions 24

2.4 Using rand and randi 25

2.5 String basics 25

2.6 String operations 27

2.7 Character vectors 29

2.8 Getting information from the user 30

Looking ahead 31

Programming Problems 31

3 Plotting 35

3.1 The plot command 35

Axis scaling 38

Plot labeling 39

3.2 Tabulating and plotting a simple function 39

3.3 Bar graphs and histograms 43

Histograms 45

3.4 Drawing several plots on one graph 45

Multiple plots with a single plot command 46

Combining multiple plots with a hold command 48

Thickening plotted curves 49

3.5 Adding lines and text 50

3.6 Changing object properties 52

Looking ahead 54

Programming Problems 55

4 Matrices 57

4.1 Entering and manipulating matrices 57

Size of a matrix 59

Matrix transpose 60

4.2 Operations on matrices 60

Arithmetic operations with a scalar 60

Addition and subtraction of two matrices of the same size 61

Functions of matrices 61

Matrix multiplication 62

The identity matrix 62

The inverse of a matrix 63

The determinant of a matrix 64

Matrix-vector multiplication 64

4.3 Solving linear systems: the backslash operator 65

Extended example: solving circuit problems 65

Wire segments 66

Wire junctions 66

Voltage sources 66

Resistors 67

Ground 67

4.4 Special matrix functions 71

Looking ahead 72

Programming Problems 72

5 Control Flow Commands 75

5.1 Conditional execution: the if statement 75

5.2 Logical expressions 79

5.3 Logical variables 80

5.4 for loops 81

Good programming practice 84

5.5 while loops 84

5.6 Other control flow commands 86

Switch-case statement 86

Break statement 86

Programming Problems 87

6 Animation 93

6.1 Basic animation 94

6.2 Animating function plots 98

6.3 Kinematics of motion 101

One-dimensional motion: constant speed 101

Motion with constant acceleration 104

Time-marching dynamics: nonconstant force 106

6.4 Looking ahead 108

Programming Problems 108

7 Writing Your Own MATLAB Functions 114

7.1 MATLAB function files 115

Declaring MATLAB functions 115

7.2 Function inputs and outputs 116

7.3 Local workspaces 117

7.4 Multiple outputs 117

7.5 Function files 117

7.6 Other functional forms 118

Subfunctions 118

Nested functions 122

Anonymous functions 122

7.7 Optional arguments for functions 123

7.8 Looking forward 124

Programming Problems 125

8 More MATLAB Data Classes and Structures 132

8.1 Cell arrays 132

8.2 Structures 133

8.3 Complex numbers 134

8.4 Function handles 135

8.5 Tables 135

8.6 Other data classes and data structures 136

Programming Problems 137

II Building Gui Tools 139

9 Building GUI Tools with App Designer 141

9.1 The App Designer interface 142

9.2 Getting started: HelloTool 144

9.3 Components communicating: SliderTool 148

9.4 Transforming a MATLAB program into a GUI tool: DampedEfieldTool 150

Step0: Write and debug the program 151

Step1: Plan the GUI 152

Step 2: Create the GUI in App Designer 153

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

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

9.5 Test and improve 157

Many ways to do things 159

Key points from this chapter 159

Programming Problems 160

10 More GUI Techniques 168

10.1 Sharing data between callbacks 169

10.2 More GUI components 170

Text and Numeric Edit Fields 170

Drop Down 171

Check Box 171

Label 172

List Box 172

Radio Button Group 173

Image 173

Communicating user choices 173

Tab Group 174

Menu bar 174

Toolbar 176

Text Area 176

The uses of invisibility 176

10.3 Popups 176

Progress dialogue 176

Wait bar 178

Input dialogue 178

Confirm dialogue 179

10.4 Responding to keyboard input 181

10.5 Mouse events and object dragging 181

III Advanced Topics 187

11 More Graphics 189

11.1 Logarithmic plots 189

11.2 Plotting functions on two axes 192

11.3 Plotting surfaces 194

11.4 Plotting vector fields 199

11.5 Working with images 200

Importing and manipulating bit-mapped images 200

Placing images on surface objects 207

11.6 Rotating composite objects in three dimensions 209

12 More Mathematics 213

12.1 Derivatives 214

Derivatives of mathematical functions expressed as MATLAB functions 214

Derivatives of tabulated functions 215

12.2 Integration 218

Integrating tabulated functions 218

Integrating mathematical functions expressed as MATLAB functions 221

12.3 Zeros of a function of one variable 225

12.4 Function minimization 227

Finding a minimum of a function of one variable 227

Multidimensional minimization 229

Fitting to an arbitrary function by multidimensional minimization 229

Solving simultaneous nonlinear equations by multidimensional minimization 233

12.5 Solving ordinary differential equations 235

Plotting a slope field 238

12.6 Eigenvalues and eigenvectors 239

13 Reading and Writing Files 242

13.1 Saving and loading data in .mat files 242

13.2 Reading and writing spreadsheet files 244

13.3 Writing text files 245

The write matrix command 245

Writing formatted text files 246

Formatting a string using sprintf 249

13.4 Reading data from a text file 249

Reading into a cell array 250

Reading complicated text data files 250

13.5 A GUI interface to filenames using uiputfile and uigetfile 252

Appendix Using latex Commands 255

Index 261

Authors

Craig S. Lent University of Notre Dame.