MATLAB Primer NotesEdit
When using Matlab, it is sometimes necessary to move to another workspace (text editor, graphic viewer, etc.) while MATLAB is still active. The !- feature can be used to prevent exiting MATLAB while performing operations in a separate program. For instance, one can edit the file EML4500.m in a local text editor by typing
When the user is finished editing the file, they will be returned to an active MATLAB window.
M-files should be stored in a dedicated directory so that they can be easily accessed by MATLAB. The default active directory of MATLAB is C:\Program Files\MATLAB\R2006\work, but the user may choose to create another active work directory. To change the active directory or to create an additional MATLAB search path, navigate to File -> set Path... and perform any necessary changes. Note that MATLAB does include subdirectories in its search path, which allows the user to save M-files in subfolders and still be able to execute them in a MATLAB session.
The table below lists commands that will be useful when working with M-files:
|path||Displays a list of all active MATLAB directory search paths|
|pwd||returns the name of the active working directory|
|cd||Used to change the working directory|
|dir or lis||Lists the contents of the active directory|
|what||Lists only the M-files in the directory|
|delete or type||Used to delete a diskfile and print an M-file to the screen|
Comparing efficiency of algorithms: Flops, tic and tocEdit
When creating computer-intensive MATLAB code, it is often wise to create an algorithm that is efficient as possible. Two way to asses the efficiency of an algorithm are the amount of floating point operations (flops) performed and the elapsed time.
The MATLAB function records the total number of flops performed. The command
flops(0) resets the flops to zero and is normally entered prior to running a an algorithm. Entering the
flops command after executing the algorithm gives the flops count for the algorithm. For example, the number of flops required to solve the equation c = a + b can be obtained with:
Flops(0), c = a + b; flops
NOTE: With MATLAB ver. 7 and newer, the
flops command can no longer be used. MATLAB will display the following error:
function f = flops(x) %FLOPS Obsolete floating point operation count. % Earlier versions of MATLAB counted the number of floating point % operations. With the incorporation of LAPACK in MATLAB 6, this % is no longer practical. % Copyright 1984-2004 The MathWorks, Inc. % $Revision: 184.108.40.206 $ $Date: 2004/01/24 09:21:21 if (nargin < 1) || (x ~= 0) warning('MATLAB:flops:UnavailableFunction', ... 'Flop counts are no longer available.') end if nargout > 0 f = 0; end
While the flops command may not work for all users, the elapsed time can still be obtained. The time required to solve an algorithm can be found by using the stopwatch timers
toc. For example, to find the total time needed to solve the equation
K=f*d, use the following command;
tic, K=f*d, toc
It should be noted, however, that the elapsed time may not be the best measure of algorithm efficiency because the rate of execution depends on how busy the processor is.
Even though all computations in MATLAB are performed in double precision, the format of the output display can be altered by the following commands:
|Format short||Fixed point with 4 decimal places (the default)|
|Format long||Fixed point with 14 decimal places|
|Format short e||Scientific notation with 4 decimal places|
|Format long e||Scientific notation with 15 decimal places|
|Format rat||approximation by ratio of small integers|
|Format hex||Hexadecimal format|
|Format bank||Fixed dollars and cents|
Once introduced, the chosen format will remain in effect until changed.
format compact will suppress blank line allowing more information to be seen on the screen.
format loose returns MATLAB to the non-compact format.