23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Primary M-File Functions<br />

Primary M-File Functions<br />

The first function in any M-file is called the primary function. Followingthe<br />

primary function can be any number of subfunctions, which can serve as<br />

subroutines to the primary function.<br />

Under most circumstances, the primary function is the only function in an<br />

M-file that you can call from the <strong>MATLAB</strong> command line or from another<br />

M-file function. You invoke this function using the name of the M-file in<br />

which it is defined.<br />

For example, the average function shown here resides in the file average.m:<br />

function y = average(x)<br />

% AVERAGE Mean of vector elements.<br />

y = sum(x)/length(x);<br />

% Actual computation<br />

You can invoke this function from the <strong>MATLAB</strong> command line with this<br />

command to find the average of three numbers:<br />

average([12 60 42])<br />

Note that it is customary to give the primary function the same name as the<br />

M-file in which it resides. If the function name differs from the filename, then<br />

you must use the filename to invoke the function.<br />

5-15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!