| Upgrading from MATLAB 4 to MATLAB 5.0 | Search  Help Desk |
Programming Capabilities
MATLAB 5.0 included flow-control improvements and new M-file programming tools.Flow-Control Improvements
MATLAB 5.0 featured:switch statement
if expressions
switch statement is a convenient way to execute code conditionally when you have many possible cases to choose from. It is no longer necessary to use a series of elseif statements:
switch input_num
case -1
disp('negative one');
case 0
disp('zero');
case 1
disp('positive one');
otherwise
disp('other value');
end
Only the first matching case is executed.
switch can handle multiple conditions in a single case statement by enclosing the case expression in a cell array. For example, assume method exists as a string variable:
switch lower(method)case {'linear','bilinear'}, disp('Method is linear')case 'cubic', disp('Method is cubic')case 'nearest', disp('Method is nearest')otherwise, disp('Unknown method.')end
| Command |
Description |
case |
Case switch. |
otherwise |
Default part of switch statement. |
switch |
Conditionally execute code, switching among several cases. |
if expressions more efficiently than before. For example, consider the expression if a|b. If a is true, then MATLAB will not evaluate b. Similarly, MATLAB won't execute statements following the expression if a&b in the event a is found to be false.M-File Programming Tools
MATLAB 5.0 added four features to enhance MATLAB's M-file programming capabilities.Variable Number of Input and Output Arguments
Thevarargin and varargout commands simplify the task of passing data into and out of M-file functions. For instance, the statement function varargout = myfun(A,B) allows M-file myfun to return an arbitrary number of output arguments, while the statement function [C,D] = myfun(varargin) allows it to accept an arbitrary number of input arguments.
Multiple Functions Within an M-File
It is now possible to have subfunctions within the body of an M-file. These are functions that the primary function in the file can access but that are otherwise invisible.M-File Profiler
This utility lets you debug and optimize M-files by tracking cumulative execution time for each line of code. Whenever the specified M-file executes, the profiler counts how many time intervals each line uses.Pseudocode M-Files
Thepcode command saves a pseudocode version of a function or script to disk for later sessions. This pseudocode version is ready-to-use code that MATLAB can access whenever you invoke the function.