MATLAB Programming
MATLAB Programming MATLAB Programming
Converting Between Strings and Cell Arrays ........... 12-31 Search and Replace Using Regular Expressions ......... 12-31 Evaluating Expressions ............................ 12-33 Find Alternatives to Using eval ...................... 12-33 Assigning to a Series of Variables .................... 12-33 Short-Circuit Logical Operators ...................... 12-33 Changing the Counter Variable within a for Loop ....... 12-34 MATLAB Path ..................................... 12-35 Precedence Rules .................................. 12-35 File Precedence ................................... 12-36 Adding a Directory to the Search Path ................ 12-36 Handles to Functions Not on the Path ................. 12-36 Making Toolbox File Changes Visible to MATLAB ....... 12-37 Making Nontoolbox File Changes Visible to MATLAB .... 12-38 Change Notification on Windows ..................... 12-38 Program Control .................................. 12-39 Using break, continue, and return .................... 12-39 Using switch Versus if .............................. 12-40 MATLAB case Evaluates Strings ..................... 12-40 Multiple Conditions in a case Statement ............... 12-40 Implicit Break in switch-case ........................ 12-40 Variable Scope in a switch .......................... 12-41 Catching Errors with try-catch ...................... 12-41 Nested try-catch Blocks ............................ 12-42 Forcing an Early Return from a Function .............. 12-42 Save and Load ..................................... 12-43 Saving Data from the Workspace ..................... 12-43 Loading Data into the Workspace .................... 12-43 Viewing Variables in a MAT-File ..................... 12-44 Appending to a MAT-File ........................... 12-44 SaveandLoadonStartuporQuit .................... 12-45 Saving to an ASCII File ............................ 12-45 Files and Filenames ................................ 12-46 Naming M-files ................................... 12-46 Naming Other Files ............................... 12-46 Passing Filenames as Arguments .................... 12-47 Passing Filenames to ASCII Files .................... 12-47 xxii Contents
Determining Filenames at Run-Time ................. 12-47 Returning the Size of a File ......................... 12-47 Input/Output ...................................... 12-49 File I/O Function Overview ......................... 12-49 Common I/O Functions ............................. 12-49 Readable File Formats ............................. 12-50 Using the Import Wizard ........................... 12-50 Loading Mixed Format Data ........................ 12-50 Reading Files with Different Formats ................. 12-51 Reading ASCII Data into a Cell Array ................. 12-51 Interactive Input into Your Program .................. 12-51 Starting MATLAB .................................. 12-52 Getting MATLAB to Start Up Faster .................. 12-52 Operating System Compatibility .................... 12-53 Executing O/S Commands from MATLAB ............. 12-53 Searching Text with grep ........................... 12-53 Constructing Paths and Filenames ................... 12-53 Finding the MATLAB Root Directory ................. 12-54 Temporary Directories and Filenames ................. 12-54 Demos ............................................ 12-55 Demos Available with MATLAB ..................... 12-55 For More Information .............................. 12-56 Current CSSM .................................... 12-56 Archived CSSM ................................... 12-56 MATLAB Technical Support ......................... 12-56 Tech Notes ....................................... 12-56 MATLAB Central ................................. 12-56 MATLAB Newsletters (Digest, News & Notes) .......... 12-56 MATLAB Documentation ........................... 12-56 MATLAB Index of Examples ........................ 12-56 xxiii
- Page 1 and 2: MATLAB ® 7 Programming
- Page 3: Revision History June 2004 First pr
- Page 6 and 7: Operating on Diagonal Matrices ....
- Page 8 and 9: Creating a Cell Array .............
- Page 10 and 11: Comma — , .......................
- Page 12 and 13: Examples of Anonymous Functions ...
- Page 14 and 15: Microsoft Excel Spreadsheets ......
- Page 16 and 17: Features of Object-Oriented Program
- Page 18 and 19: Object Precedence .................
- Page 20 and 21: 12 Programming Tips Command and Fun
- Page 24 and 25: A External Interfaces Finding the D
- Page 26 and 27: 1 Data Structures Empty Matrices, S
- Page 28 and 29: 1 Data Structures (shown as E below
- Page 30 and 31: 1 Data Structures 0 0 0 0 0 0 0 0 0
- Page 32 and 33: 1 Data Structures 0.6068 0.7621 0.8
- Page 34 and 35: 1 Data Structures 3 5 7 3 5 7 3 5 7
- Page 36 and 37: 1 Data Structures A = 3:0.2:3.8 A =
- Page 38 and 39: 1 Data Structures 5000 50 B = [int8
- Page 40 and 41: 1 Data Structures class(x) ans = si
- Page 42 and 43: 1 Data Structures Matrix Indexing T
- Page 44 and 45: 1 Data Structures A used in the pre
- Page 46 and 47: 1 Data Structures 16 5 9 4 . . . 12
- Page 48 and 49: 1 Data Structures Getting Informati
- Page 50 and 51: 1 Data Structures Function islogica
- Page 52 and 53: 1 Data Structures This example runs
- Page 54 and 55: 1 Data Structures A = 10 20 30 40 5
- Page 56 and 57: 1 Data Structures This changes matr
- Page 58 and 59: 1 Data Structures A = [1+9i 2-8i 3+
- Page 60 and 61: 1 Data Structures rp = rp + blocksi
- Page 62 and 63: 1 Data Structures A = [1:8; 11:18;
- Page 64 and 65: 1 Data Structures rand('state', 0);
- Page 66 and 67: 1 Data Structures OperatingonDiagon
- Page 68 and 69: 1 Data Structures Empty Matrices, S
- Page 70 and 71: 1 Data Structures These operations
Converting Between Strings and Cell Arrays ........... 12-31<br />
Search and Replace Using Regular Expressions ......... 12-31<br />
Evaluating Expressions ............................ 12-33<br />
Find Alternatives to Using eval ...................... 12-33<br />
Assigning to a Series of Variables .................... 12-33<br />
Short-Circuit Logical Operators ...................... 12-33<br />
Changing the Counter Variable within a for Loop ....... 12-34<br />
<strong>MATLAB</strong> Path ..................................... 12-35<br />
Precedence Rules .................................. 12-35<br />
File Precedence ................................... 12-36<br />
Adding a Directory to the Search Path ................ 12-36<br />
Handles to Functions Not on the Path ................. 12-36<br />
Making Toolbox File Changes Visible to <strong>MATLAB</strong> ....... 12-37<br />
Making Nontoolbox File Changes Visible to <strong>MATLAB</strong> .... 12-38<br />
Change Notification on Windows ..................... 12-38<br />
Program Control .................................. 12-39<br />
Using break, continue, and return .................... 12-39<br />
Using switch Versus if .............................. 12-40<br />
<strong>MATLAB</strong> case Evaluates Strings ..................... 12-40<br />
Multiple Conditions in a case Statement ............... 12-40<br />
Implicit Break in switch-case ........................ 12-40<br />
Variable Scope in a switch .......................... 12-41<br />
Catching Errors with try-catch ...................... 12-41<br />
Nested try-catch Blocks ............................ 12-42<br />
Forcing an Early Return from a Function .............. 12-42<br />
Save and Load ..................................... 12-43<br />
Saving Data from the Workspace ..................... 12-43<br />
Loading Data into the Workspace .................... 12-43<br />
Viewing Variables in a MAT-File ..................... 12-44<br />
Appending to a MAT-File ........................... 12-44<br />
SaveandLoadonStartuporQuit .................... 12-45<br />
Saving to an ASCII File ............................ 12-45<br />
Files and Filenames ................................ 12-46<br />
Naming M-files ................................... 12-46<br />
Naming Other Files ............................... 12-46<br />
Passing Filenames as Arguments .................... 12-47<br />
Passing Filenames to ASCII Files .................... 12-47<br />
xxii<br />
Contents