M-files (Script files)
· So
far in our introduction to MATLAB we have worked in an interactive mode,
executing MATLAB commands in the MATLAB command window. Although this makes it
convenient to test and explore some basic operations, the real power of MATLAB,
like many other programming languages, rests in our ability to construct
sequences of commands that can be executed repeatedly and easily modified. To
do this in MATLAB we construct script files or m-files using the MATLAB editor.
·
To illustrate this process, we'll use a copy of
the code that we have constructed for the Piston Motion Example, which has been
put into an m-file. We modify it to
complete Problem 1.5-2 at the end of Chapter 1.
· In
the MATLAB command window, select File->New->M-file. This will bring up the m-file editor (just a
text editor with a few MATLAB specific features). Using the mouse, copy-and-paste the following commands into the
m-file editor…
%
% Example 1.5: Piston Motion Calculation
%
clear all; close all; clc;
% Define known parameters
L1=1.0;
L2=0.5;
R=L2/L1;
% Set range of calculation
Ad=[0:.05:180];
Ar=Ad*pi/180;
% Calculate the output
B=asin(R*sin(Ar));
d=L1*cos(B)+L2*cos(Ar);
% Plot and label the results
figure(1)
plot(Ad,d);
grid;
title('Locus of Piston in Example
1.5-1 (Pg. 19)')
xlabel('Crankshaft Angle, A
(degrees)')
ylabel('Piston Travel, d (ft)')
% Extension to deal with end of chapter question
L1=0.5;L2=0.5;R=L2/L1;
B=asin(R*sin(Ar));
d1=L1*cos(B)+L2*cos(Ar);
L1=1.5;L2=0.5;R=L2/L1;
B=asin(R*sin(Ar));
d2=L1*cos(B)+L2*cos(Ar);
figure(2)
plot(Ad,d,Ad,d1,Ad,d2);
grid;
legend('L1=1.0','L1=0.5','L1=1.5')
title('Locus of Piston in Example
1.5-1 (Pg. 19)')
xlabel('Crankshaft Angle, A
(degrees)')
ylabel('Piston Travel, d (ft)')
· Now
save the content in the m-file editor as piston.m
(using File->Save). It will, by
default, save this file in your z:\matlab
directory. If you type dir (or ls) in the
command window, you should now see the file piston.m in your
z:\matlab directory (which is your current directory by default).
· You
will notice that the first few lines of the file piston.m begin
with a % symbol.
This symbol tells MALTAB that the rest of this line is a comment statement. The
collection of comment statements at the beginning of an m-file is used to
provide the help information for the file. Typing help piston in the MATLAB command window should display the
first set of comment lines in the m-file.
· To
have MATLAB execute your script file (m-file),
type the name of the script file, without the .m
extension at the command prompt. If it
is in the path or current directory, MATLAB will execute the sequence of
commands in your file (as if you were typing them in by hand). If you have edited the m-file, be sure to
save the edited version prior to executing the m-file, or the old version will
run.
· To
access a file, which may be stored in a subdirectory or some directory other
than z:\matlab, we need to tell MATLAB where to find it. The sequence of directories that MATLAB
searches to find MATLAB m-files is referred to as the MATLAB path. You can view
this sequence by simply typing the command path at the MATLAB prompt.
·
You add your own directories to this path using
the path command. Here is an example of
adding z:\egr101 to your path (assuming this directory exists):
path(path, 'z:\egr101')
Alternatively,
we can use the path browser to add directories to the path.
·
MATLAB always searches the current directory as
well as those directories in its path.
To display and change your current directory, use the following
commands:
pwd -
displays the current directory
cd z:\egr101 -
changes the current directory to z:\egr101
· The
first command line that appears in the file piston.m
contains a few commands that prove to be fairly useful, especially when you are
learning to write m-files for the first time. They are separated by ;s
which allow us to include more than one command on each line. The three
commands do the following:
clear all -
clears all variables from the MATLAB workspace
close all - closes all
open figure windows
clc - clears the
MATLAB command window
· Table
2.1-4 (Pg. 33) details some other commands that can be fairly useful for
managing your MALTAB work session.
· MATLAB
follows a typical naming convention when you are creating and using script or
m-files. A valid filename begins with a letter and may include letters, digits and
the underscore character. Filenames are limited to 19 characters and all
m-files have the file extension .m
· In
naming m-files you should avoid common words and mathematical terms since these
are probably already used by MATLAB. When in doubt about a particular filename,
MATLAB allows you to check to see if a particular file already exists somewhere
in the path before creating the file. To do this you simply type:
exist('plot')
exist('myplot')
If the file does not exist the command returns a zero (0) to the command window. A non-zero number will appear if it already exists (meaning pick a new name).