Lab Session 05: Script File
Lab Session 05: Script File
Script File
A script file is a list of MATLAB commands, called a program that is saved in a file. When
a script file is executed, MATLAB executes the commands in the order they are written just as if
they were typed in the Command Window. When a script file has a command that generates an
output (e.g., assignment of a value to a variable without a semicolon at the end), the output is
displayed in the Command Window. Using a script file is convenient because it can be edited
(corrected or\ otherwise changed) and executed many times. Script files can be typed and edited
in any text editor and then pasted into the MATLAB editor. Script files are also called M-files
because the extension .m is used when they are saved.
Once the window is open, the commands of the script file are typed line by line. MATLAB
automatically numbers a new line every time the Enter key is pressed. The commands can also be
typed in any text editor or word processor program and then copied and pasted in the
Editor/Debugger Window. An example of a short program typed in the Editor/Debugger Window
1
is shown in Figure. The first few lines in a script file are typically comments (which are not
executed since the first character in the line is %) that describe the program written in the script
file.
Before a script file can be executed it has to be saved. This is done by choosing Save As...
from the File menu, selecting a location (many students save to a flash drive, which appears in the
directory as Drive(F:) or (G:)), and entering a name for the file. When saved, MATLAB adds the
extension .m to the name. The rules for naming a script file follow the rules of naming a variable
(must begin with a letter, can include digits and underscore, no spaces, and up to 63 characters
long). The names of user-defined variables, predefined variables, and MATLAB commands or
functions should not be used as names of script files.
2
Current Folder
The current folder is shown in the “Current Folder” field in the desktop toolbar of the
Command Window, as shown in Figure.
If an attempt is made to execute a script file by clicking on the Run icon (in the Editor
Window) when the current folder is not the folder where the script file is saved, then the prompt
shown in Figure.
The user can then change the current folder to the folder where the script file is saved, or
add it to the search path. Once two or more different current folders are used in a session, it is
possible to switch from one to another in the Current Folder field in the Command Window. The
current folder can also be changed in the Current Folder Window, shown in Figure below, which
can be opened from the Desktop menu. The Current Folder can be changed by choosing the drive
and folder where the file is saved.
3
An alternative simple way to change the current folder is to use the cd command in the
Command Window. To change the current folder to a different drive, type cd, space, and then the
name of the directory followed by a colon : and press the Enter key. For example, to change the
current folder to drive F (e.g., the flash drive) type cd F:. If the script file is saved in a folder within
a drive, the path to that folder has to be specified. This is done by typing the path as a string in the
cd command. For example, cd('F:\Chapter 1') sets the path to the folder Chapter 1 in drive F. The
following example shows how the current folder is changed to be drive E. Then the script file from
Figure, which was saved in drive E as ProgramExample.m, is executed by typing the name of the
file and pressing the Enter key.
Practice Exercise
Write a script file (program) for the following problems and also show the ouput;
4
1. Radioactive decay of carbon-14 is used for estimating the age of organic material. The
decay is modeled with the exponential function f(t) = f(0)ekt, where t is time, f(0) is the
amount of material at t = 0, f(t) is the amount of material at time t, and k is a constant.
Carbon-14 has a half-life of approximately 5,730 years. A sample taken from the ancient
footprints of Acahualinca in Nicaragua shows that 77.45% of the initial ( t = 0) carbon-14
is present. Determine the estimated age of the footprint. Solve the problem by writing a
program in a script file. The program first determines the constant k, then calculates t for
f(t) = 0.7745f(0), and finally rounds the answer to the nearest year.
2. The Moment Magnitude Scale (MMS), denoted Mw, which measures the total energy
released by an earthquake, is given by:
2
MW log10 M 0 10.7
3
where M0 is the magnitude of the seismic moment in dyne-cm (measure of the energy
released during an earthquake). Determine how many times more energy was released from
the largest earthquake in the world, in Chile (Mw = 9.5), 1960, than the earthquake in
Balakot, Pakistan (Mw = 7.6), in 2005.
3. According to special relativity, a rod of length L moving at velocity v will shorten by an
amount δ , given by:
v2
L 1 1 2
c
where c is the speed of light (about 300 x 106 m/s). Calculate how much a rod 2 m long
will contract when traveling at 5,000 ms-1.
4. The value B of a principal P that is deposited in a saving account with a fixed annual interest
rate r after n years can be calculated by the formula:
nm
r
B P 1
m
5
where m is the number of times that the interest is compounded annually. Consider a
$80,000 deposit for 5 years. Determine how much more money will be earned if the interest
is compounded daily instead of yearly.
5. Newton's law of cooling gives the temperature T(t) of an object at time tin terms of T0, its
temperature at t = 0, and Ts, the temperature of the surroundings.
T (t ) Ts (T0 Ts )e kt
A police officer arrives at a crime scene in a hotel room at 9:18 PM, where he finds a dead
body. He immediately measures the body's temperature and find it to be 79.5° F. Exactly
one hour later he measures the temperature again, and find it to be 78.0° F. Determine the
time of death, assuming that victim body temperature was normal ( 98.6° F) prior to death,
and that the room temperature was constant at 69° F.