The simplest type of MATLAB® program is called a script.
A script is a file with a
.m extension that contains
multiple sequential lines of MATLAB commands and function calls.
You can run a script by typing its name at the command line.
To create a script, use the
This opens a blank file named
Enter some code that plots a vector of random data:
n = 50; r = rand(n,1); plot(r)
Next, add code that draws a horizontal line on the plot at the mean:
m = mean(r); hold on plot([0,n],[m,m]) hold off title('Mean of Random Uniform Data')
Whenever you write code, it is a good practice to add comments
that describe the code. Comments allow others to understand your code,
and can refresh your memory when you return to it later. Add comments
using the percent (
% Generate random data from a uniform distribution % and calculate the mean. Plot the data and the mean. n = 50; % 50 data points r = rand(n,1); plot(r) % Draw a line from (0,m) to (n,m) m = mean(r); hold on plot([0,n],[m,m]) hold off title('Mean of Random Uniform Data')
Save the file in the current folder. To run the script, type its name at the command line:
You can also run scripts from the Editor by pressing the Run button, .
Within a script, you can loop over sections of code and conditionally
execute sections using the keywords
For example, create a script named
for loop to calculate the mean of five random
samples and the overall mean.
nsamples = 5; npoints = 50; for k = 1:nsamples currentData = rand(npoints,1); sampleMean(k) = mean(currentData); end overallMean = mean(sampleMean)
Now, modify the
for loop so that you can
view the results at each iteration. Display text in the Command Window
that includes the current iteration number, and remove the semicolon
from the assignment to
for k = 1:nsamples iterationString = ['Iteration #',int2str(k)]; disp(iterationString) currentData = rand(npoints,1); sampleMean(k) = mean(currentData) end overallMean = mean(sampleMean)
When you run the script, it displays the intermediate results, and then calculates the overall mean.
Iteration #1 sampleMean = 0.3988 Iteration #2 sampleMean = 0.3988 0.4950 Iteration #3 sampleMean = 0.3988 0.4950 0.5365 Iteration #4 sampleMean = 0.3988 0.4950 0.5365 0.4870 Iteration #5 sampleMean = 0.3988 0.4950 0.5365 0.4870 0.5501 overallMean = 0.4935
In the Editor, add conditional statements to the end of
display a different message depending on the value of
if overallMean < .49 disp('Mean is less than expected') elseif overallMean > .51 disp('Mean is greater than expected') else disp('Mean is within the expected range') end
calcmean and verify that the correct
message displays for the calculated
overallMean = 0.5178 Mean is greater than expected
MATLAB looks for scripts and other files in certain places. To run a script, the file must be in the current folder or in a folder on the search path.
By default, the
MATLAB folder that the MATLAB Installer
creates is on the search path. If you want to store and run programs
in another folder, add it to the search path. Select the folder in
the Current Folder browser, right-click, and then select Add