Department of Engineering

IT Services

Programs

Rather than entering text at the prompt, MATLAB can get its commands from a .m file. If you type edit prog1, Matlab will start an editor for you. Type in the following and save it.


for x=1:10

y(x)=x^2+x;
end
y

The step term in the for statement defaults to 1 when omitted. Back inside MATLAB run the script by typing:


>> prog1

which should result in vector y being displayed numerically. Typing


>> plot(y)

>> grid

should bring up a figure displaying y(x) against x on a grid. Like many matlab routines plot can take a variable number of arguments. With just one argument (as here) the argument is taken as a vector of y values, the x values defaulting to 1,2,..., Note the effect of resizing the figure window.

The Teaching System is set up so that if you have a directory called matlab in your home directory, then .m scripts there will be run irrespective of which directory you were in when you started matlab.