Using Matlab as a Research Tool
2. Matrix operations, save/load, script files, user functions
Hanselman & Littlefield: Chapters 4 (Script
M-files), 12 (Functions), 14 (File and directory
management), 18 (Data analysis)
Matrix functions that operate by column
– sum, mean, prod
– cumsum, cumprod
– operations on vectors
– grand sums, means, etc.
– operating by row via double transposition
M-files (script files)
– global variables
– comments
– Ex: write MatrixTotals as a script file
M-files (functions)
– sample functions: LogSpiral, AnovaIsotonic
– global vs. local variables
– function, return
– input arguments
– output arguments
– comments
– help documentation
Ex: MatrixTotals, CricketChirpsToTemp, TimeConvert, GetLineParams, RandomIntegers
Elements of a well-written function
– help documentation
– name and purpose of function
– usage
– descriptions of input and output variables
– meaningful function and variable names in standardized format
– indentation and comments (becomes more important later)
– blank lines separating groups of related statements
– error-checking up front, with meaningful error messages
Saving and loading
– diary
– diary fname
– diary on, off
– save [matfile]
– save [fname.mat]
– save fname
– save fname [vars]
– save [ascii]
– save fname [vars] -a
– load [matfile]
– load [fname.mat]
– load fname
– load [ascii]
– load xxx.yyy, produces a matrix xxx
– xlsread(‘filename’)
– “copy and paste” instead of load