next Loops and Conditional Statements
up Music 270a: Matlab Tutorial 2
previous Music 270a: Matlab Tutorial 2


Creating functions

In addition to making scripts, we can also create functions. If they are stored in a location that is in the Matlab path, they can be used as a toolkit to help limit repeated code used for common tasks.

Matlab functions are also created in an m-file, however the file is initiated with the following

function [output_parameters] = <function_name>(input_parameters}

The $ <functionname>$ with an ``.m'' extension must be the name of the m-file. To create a function that adds two numbers for example, your function may be called ``myadd'', and your m-file would be called ``myadd.m'' and would contain the following

function y = myadd(a, b)

% MYADD -- adds two numbers a and b
%
% See also SUM.

y = a+b;


next Loops and Conditional Statements
up Music 270a: Matlab Tutorial 2
previous Music 270a: Matlab Tutorial 2

``Music 270a: Digital Audio Processing'' by Tamara Smyth, Department of Music, University of California, San Diego (UCSD).
Download PDF version (matlabtut2.pdf)
Download compressed PostScript version (matlabtut2.ps.gz)

Copyright © 2013-01-08 by Tamara Smyth.
Please email errata, comments, and suggestions to Tamara Smyth<trsmyth@ucsd.edu>