Script m-Files

If you have to execute a series of commands repeatedly, it might be better to write the commands in a so called `script file'. The file should have a name with the extension `.m'.
As an example, let us consider the drawing of the function $ f(x) = \vert x \sin(2 \pi x) \vert$ on the interval [0,2] by using a `script file': Through the menu `File $ \backslash$ New $ \backslash$ M-file' the `MATLAB Editor/Debugger' is opened. Type the following lines:
x = 0:0.1:2;
y = abs(x.*sin(2*pi*x));
plot(x,y)
title('f(x)= |x sin(2 pi x)| on the interval [0,2]')
xlabel('x')
ylabel('f(x)')
axis([0 2 0 2])
shg
Save the file through the menu `File $ \backslash$ Save' under the name `picture.m'. If you now give the command picture in MATLAB, then the graph of $ f(x)$ on the interval [0,2] will appear. An other option to save and run a file written in your editor window is to press the button \includegraphics[width=.4cm]{ch1_run} on top of it. Be careful, that this will overwrite a possible previous file.

It is better to save the commands that generate a figure than to save the figure itself.



Previous      Next      Up      Contents


Esteur 2010-03-22