12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

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.

% Do the valleys:subplot(222)ind = find(z>0);zplot = z;zplot(ind) = zeros(size(ind));mesh(zplot)axis tightNow try this in the matlab window:mfile1matlab has executed the instructions in mfile1.m just as if you hadtyped them in. The lines beginning with the percent sign % are ignoredby matlab so they can be used to put comments in your code. Blanklines can be used to improve readability.Any variables created by a script m-file are available in the comm<strong>and</strong>window after the m-file completes running:>> clear>> whos>> mfile1>> whosName Size Bytes Classind 1544x1 12352 double arrayz 49x49 19208 double arrayzplot 49x49 19208 double arrayGr<strong>and</strong> total is 6346 elements using 50768 bytesThese variables are said to exist in the matlab workspace. Scripts canalso operate on variables that already exist in the workspace.You can type the name <strong>of</strong> a script file within another script file. Forexample you could create another file called mfile2 that contains thetext line mfile1; the contents <strong>of</strong> mfile1 will then be executed at thatpoint within mfile2.8.2 FunctionsFunctions are m-files that can be used to extend the matlab language.Functions can accept input arguments <strong>and</strong> produce output arguments.Many <strong>of</strong> matlab’s own comm<strong>and</strong>s are implemented as m-files; try typingtype mean to see how matlab calculates the mean. Functions usec○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!