University of Florida/Eml4500/f08.qwiki.harris

MATLAB Primer Notes
Link to MATLAB Primer: http://apollo.mae.ufl.edu/~vql/courses/feem/matlab_primer_3rd.pdf

Managing M-Files
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:

Comparing efficiency of algorithms: Flops, tic and toc
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  resets the flops to zero and is normally entered prior to running a an algorithm. Entering the  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:

NOTE: With MATLAB ver. 7 and newer, the  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: 5.12.4.1 $  $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  and. For example, to find the total time needed to solve the equation, use the following command;

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.

Output format
Even though all computations in MATLAB are performed in double precision, the format of the output display can be altered by the following commands:

Once introduced, the chosen format will remain in effect until changed.

The command  will suppress blank line allowing more information to be seen on the screen.

The command  returns MATLAB to the non-compact format.