Handy commands that are useful in script files include:
>> type hilb function H = hilb(n) %HILB Hilbert matrix. % HILB(N) is the N by N matrix with elements 1/(i+j-1), % which is a famous example of a badly conditioned matrix. % See INVHILB for the exact inverse. % % This is also a good example of efficient MATLAB programming % style where conventional FOR or DO loops are replaced by % vectorized statements. This approach is faster, but uses % more storage. % C. Moler, 6-22-91. % Copyright (c) 1984-96 by The MathWorks, Inc. % $Revision: 5.4 $ $Date: 1996/08/15 21:52:09 $ % I, J and E are matrices whose (i,j)-th element % is i, j and 1 respectively. J = 1:n; J = J(ones(n,1),:); I = J'; E = ones(n,n); H = E./(I+J-1); >>Recall the warning earlier: help files give the name in capital letters, but the Matlab hilb function must be used in lower case. Now note what happens when you give the help command:
>> help hilb
HILB Hilbert matrix.
HILB(N) is the N by N matrix with elements 1/(i+j-1),
which is a famous example of a badly conditioned matrix.
See INVHILB for the exact inverse.
This is also a good example of efficient MATLAB programming
style where conventional FOR or DO loops are replaced by
vectorized statements. This approach is faster, but uses
more storage.
The first line of a function M-file starts with the keyword
function. Any comment lines following it are printed out when
you use help on the function - so that is the place to
put comments about usage. Note that the help lines to be
displayed ended with
the first blank line, or with the first non-commented line in
general.
The hilb function takes one argument (the order of the matrix) and returns one argument (the matrix). If you invoke it as
A = hilb(10);the matrix H is stored in A. If you invoke it as
hilb(10);the result in stored in ans. Matlab allows variable numbers of arguments and return values. Inside the function, you can test for the number of input and output arguments the function was called with using nargin and nargout, resp. As an example of this, look at the function normfit, using both help and type commands.
Here is a gotcha in functions: the first time you execute an M-file function, Matlab compiles it into an internal representation. That allows subsequent invocations to run much faster. On following calls in the same session, it uses that one. Why is this a gotcha? There you are, running Matlab in one window and editing the function in another. You run it, then make a change in the editing window. You run it again, and the change does not show up. Matlab will compare the timestamp of the modified function file and the timestamp of its compiled version. It will recompile if the function file is newer than the compiled version. However, if you type fast you can encounter the situation where the timestamp on the modified file is the same as that of the compiled function - if the gap is less than a second. Beware of this - especially if you are using NFS-mounted files, and working from two different computers. In that case, a few seconds clock skew between the computers may lead to your changes seeming to not be implemented.
When a function has more than one output value that you want to capture, you use the array bracket notation:
[L,U,P] = lu(A);This will create (or overwrite) arrays L, U, and P.
Another Big Gotcha: When a function is called, its input variables are not copied into the function workspace but their values are readable by the function (call by reference). However, if the function changes any entry in an input argument array, that array will be copied into the function's workspace (call by value). Furthermore, if a variable appears as both an input and output argument, then it is copied into the function's workspace. Conclusion: if you have a humongous array and a function that changes just two values in it, you are better off to extract the elements and call the function, rather than having the humongous array copied over to the function's workspace, and then copied back to the global workspace.
You can get around this by defining the variable to be global, using the global keyword. Don't do that in general, it's bad practise.