It's highly subjective to say just what constitutes an ``intermediate'' versus a ``basic'' programming feature.
The preceding section, Programming Basics, covers what you need to get started with writing MATLAB functions. Soon after you get comfortable with the process of using and writing functions, I believe you will want to incorprate the features described below.
evalto provide an error trap
For all but the most trivial MATLAB functions, I usually write an entensive function prologue that will be returned by the help facility. The prologue contains a brief statement of the what the function does, a synopsis of how the function may be called, and complete descriptions of all input and output parameters.
As an example, here is the prologue to readColData.m function, which is described in the section Plotting data from files with column headings .
function [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % readColData reads data from a file containing data in columns % that have text titles, and possibly other header text % % Synopsis: % [labels,x,y] = readColData(fname) % [labels,x,y] = readColData(fname,ncols) % [labels,x,y] = readColData(fname,ncols,nhead) % [labels,x,y] = readColData(fname,ncols,nhead,nlrows) % % Input: % fname = name of the file containing the data (required) % ncols = number of columns in the data file. Default = 2. A value % of ncols is required only if nlrows is also specified. % nhead = number of lines of header information at the very top of % the file. Header text is read and discarded. Default = 0. % A value of nhead is required only if nlrows is also specified. % nlrows = number of rows of labels. Default = 1 % % Output: % labels = matrix of labels. Each row of lables is a different % label from the columns of data. The number of columns % in the labels matrix equals the length of the longest % column heading in the data file. More than one row of % labels is allowed. In this case the second row of column % headings begins in row ncol+1 of labels. The third row % column headings begins in row 2*ncol+1 of labels, etc. % % NOTE: Individual column headings must not contain blanks % % x = column vector of x values % y = matrix of y values. y has length(x) rows and ncols columns
evalto provide an error trap
evalcommand can be used to provide an error trap. This is one way to recover more gracefully from an error condition, especially if the error occurs in a built-in MATLAB function.
The syntax of the
eval error trap is
desiredis the m-file you wish to execute and
fallBackis the name of the m-file (or MATLAB command) that is executed if an error occurs in the
Normally when MATLAB encounters an error it prints a message
and the name of the function in which the error occured. It
then stops. With the
eval error trap, MATLAB
will continue execution. Presummably some meaningful recovery
operation is provided by the
fallBack function or
text. If all you want to do is provide and error message use,
error function instead.
Refer to the version 4.1 (or version 4.2) release notes for
a nice example of using
to build an error trap.
[Preceding Section] [Master Outline] [Section Outline] [Next Section]