Startup and Shutdown :
Matlab (Windows) : Start MATLAB program (Windows platforms)
Exit : Terminate MATLAB program (same as quit)
Matlabrc : Master Startup M-file for MATLAB -automatically executed by MATLAB during startup
Startup : Startup file for user-defined options.
finish : Termination file for MATLAB program
Basic Settings :
Prefdir : Preference directory name. returns directory containing preferences for MATLAB and related products.
Preferences : Open user settable Preferences dialog.
Help and Support :
Doc : Reference page in Help browser
help : Help for functions in Command Window
docsearch : Help browser search
lookfor : Search for keyword in all help entries
version : Version number for MATLAB and libraries
Ver : Version information for MathWorks products
MATLAB Commands :
Code Performance:
Bench : MATLAB Benchmark
cputime : Elapsed Cpu time in Seconds.
Memory : Display memory information on how much available and how much being used.
Profile : Profile execution time for function
profsave : Save profile report in HTML format.
Memory Usage :
inmem : Names of functions, MEX-files, classes in memory.
memory : Displays Memory Information
pack : Consolidated memory space.
Workspace :
Clear : Remove items from workspace, freeing up system memory
Clearvars : Clear variables from memory.
disp : Display text or array.
openvar : Open workspace variable in variables editor or other graphical editing tool.
Who : list variables in workspace.
Whos : List variables in workspace, with sizes and types.
Load : Load Data from MAT-file into workspace.
Save : Save Workspace variables to files.
Mat File : Load and save parts of variables in MAT-files
Workspace : Open Workspace browser to manage workspace
M-Files : Script and Function Files
When problems become complicated and require re–evaluation, entering command at MATLAB prompt is not practical
Solution : use M-files
Script : Collection of Commands executed in sequence when called.
Saved with extension “.m”
Function : User Defined Commands Normally has input & Output.
Saved with the extension “.m”
What id M -File ?
- Simple text file containing MATLAB commands
- Executes Matlab commands sequentially
- Identified by ‘.m‘ extension – in Windows OS (e.g. test.m)
Advantages of M-Files ?
- Easy editing and saving of work
- Undo changes
- Readability/Portability – non executable comments can be added using the ‘%’ symbol to make commands easier to understand
- Saving M-files is far more memory efficient than saving a workspace
How to Execute ?
- By typing filename at the MATLAB command prompt – It will be executed provided that the saved file is in the known path
- From Scripting editor, by clicking run button
At Matlab prompt type in edit to invoke M-file editor
Syntax: function [y1,…,yN] = myfun(x1,…,xM)
- Declares a function named myfun that accepts inputs x1,…,xM and returns outputs y1,…,yN.
- This declaration statement must be the first executable line of the function
- Save the function code in a text file with a .m extension
- The name of the file should match the name of the first function in the file
Anonymous Functions:
- contain only a single executable statement
- associated with a variable
- not stored in a program file
Example:
sqr = @(x) x.^2;
a = sqr(5)
a = 25
Scripting Basics :
if/elseif/else : Execute statements if condition is true
for : Execute statements specified number of times
Switch/case/Otherwise : Switch among several cases based on expression
break : Terminate execution of for or while loop
Condition Statements
- It is often necessary to only perform matlab operations when certain conditions are met
- Relational and Logical operators are used to define specific conditions
- Simple flow control in matlab is performed with the ‘If’, ‘Else’, ‘Elseif’ and ‘Switch’ statements
If, Else, and Elseif
- An if statement evaluates a group of commands when the logical expression is evaluated as true
- The list of conditional commands are terminated by the end statement
- If the logical expression is evaluated as false, all the conditional commands are skipped
- Execution of the script resumes after the end statement
Basic form:
if logical_expression
commands
end