MATLAB Script Files

You can put a sequence of MATLAB commands into a simple script file as an alternative to typing the sequence again.  To do this, select either 'File/New' or the icon that looks like a document.  The MATLAB Editor opens and you can enter MATLAB commands.

Try this one:
 
x = 0: .01 : 5;
y = sin (x) .* cos (5*x);
plot(x, y)

Now save the file.  You are asked for a file name.  Try the name sines.  MATLAB will store this file with the name sines.m.  This m extension indicates a MATLAB file.

You can use your scipt at the command prompt.  Just type sines or sines.m, and MATLAB runs your script.
 

Here is a slightly more complicated script.  It contains MATLAB comments, as well as the MATLAB commands for text input, text output, and pause.
 
% MATLAB example script
 % produces a plot based on user input

 % get interval for x-axis
 start = input('Please enter the coordinate for the left-hand side of the interval> ');
 stop = input('Please enter the coordinate for the right-hand side of the interval> ');

 % compute x and y arrays, then display them
 x = start : .1 : stop ;
 y = sin (x) .* cos (5*x);
 disp('Here are the data points')
 [x; y]'

 % create a pause
 disp('Press any key to see the plot')
 pause

 % create a plot of y as a function of x
 plot(x, y)
 grid
 title('The product of sines')
 xlabel('The x axis')
 ylabel('The y axis')

Some rules about MATLAB script files