MATLAB Functions
MATLAB Functions
To open a new m-file: In the MATLAB command window, go to FILE on the toolbar, select
NEW, then select M-FILE. This opens the MATLAB editor/debugger and gives an empty file in
which you can create whatever m-file you want.
where Out_1,Out_2,…,Out_N are the N output variables and In_1,In_2,…,In_M are the
M input variables;
1
Code in an m-file will be shown here using the Arial font, e.g., x = cos(0.1*pi*n)
y=In_1.^2;
This takes the values in In_1, squares them, and assigns the result to the variable y.
Note: putting a semicolon at the end of an expression stops the display of the result – a good idea
unless you really WANT to see the result (sometimes useful when debugging or verifying a
function).
Out_1=cos(y);
will compute the cosine of the values in the variable y and then assigns the result to the variable
Out_1, which will then be output by the function (assuming that Out_1 was specified as an
output variable name).
Then, once you have it running without any syntax errors or warnings, you need to test it to
verify that it really does what you intended it to do. Obviously, for simple functions you may be
able to verify that it works by running a few examples and checking that the outputs are what
you expect. Usually you need to do quite a few test cases to ensure that it is working correct.
For more complex functions (or when you discover that the outputs don’t match what you
expect) you may want to check some of the intermediate results that you function computes to
verify that they are working properly. To do this you set “breakpoints” that stop the operation of
the function at a specified line and allow you then view from the command:
• Now when you run your function, it will stop at that line and will put you into a
mode that will allow you to view all the variables at that point
• When you are stopped (i.e., “in debug mode”) you have a different prompt than
MATLAB’s standard >> prompt
o When you are all done using this feature you can turn it off by repeating
the process used to set the breakpoint
Once you have it stopped, any variable that has been computed up to that point can be inspected
(plot it, look at its values, check its size, check if it is row or column, etc. You can even modify
a variable’s contents to correct a problem). Note that the ONLY variable you have access to are
those that have been created inside the function or have been passed to it via the input arguments.
Note that you can set multiple breakpoints at a time – once you have stopped at the first one you
can click on DEBUG and then click on CONTINUE and it will pick up execution immediately
where it left off (but with the modified variables if you changed anything).
Note also that once you have a function stopped in debug mode you can “single-step” through
the next several lines: click on DEBUG and click on SINGLE STEP.
3. It is often helpful to put several comment lines right after the function definition line. These
comments explain what the function does, what the inputs and outputs are, and how to call
the function.
4. Putting in lots of comments helps you and others understand what the function does – from a
grading point of view you will have a higher probability of getting full credit if you write
comments that tell me what your code is doing
An Example
Here is an example of a MATLAB function. Suppose you would like to write a general routine
that will compute the value of some arbitrary polynomial at an arbitrary set of equally-spaced x
values. This turns out to be something that is best done using loops (or at least, I couldn’t find a
clever way to avoid loops – this is good because it at least gives an example of how to do loops).
The function might look like this:
for n=0:power
% Loop through the powers in the polynomial
% and compute a row vector for each of the terms in the polynomial
% and put that row vectro into each row of matrix X
X(n+1,:)=coeffs(n+1)*x.^n; % note use of "n+1" for indexing - matlab
can't index using "0"
end
% You now have a matrix X who's nth row is C_n*x.^n
% The x values run across this matrix, the power value runs down this matrix
» [y_0,x_0] = poly_equi(0,3,-5.1,5.0,0.2);
» [y_1,x_1] = poly_equi(1,[3 2],-5.1,5.0,0.2);
» [y_2,x_2] = poly_equi(2,[3 2 2],-5.1,5.0,0.2);
» subplot(3,1,1)
» plot(x_0,y_0)
» xlabel('x values')
» ylabel('y values')
» subplot(3,1,2)
» plot(x_1,y_1)
» xlabel('x values')
» ylabel('y values')
» subplot(3,1,3)
» plot(x_2,y_2)
» xlabel('x values')
» ylabel('y values')
Note that we ran the function three times – each time we put in different values for the input
variables and called the output variables something different each time so that we could store the
three different results. We then plotted the results on three different subplots and labeled the
axes.