[ SCHOOL LOGO ]
FEDERAL UNIVERSITY OF TECHNOLOGY, IKOT ABASI
DEPARTMENT OF AEROSPACE ENGINEERING
ASE 202 — MATLAB PROGRAMMING
SCRIPT FILES
Explain the Term Script File and the Procedure for Creating a Script File in MATLAB
GROUP D
Group Members
[ To be filled in — Name and Matric Number for each member ]
Introduction & Objectives
This presentation covers the concept of Script Files in MATLAB® — what they are, why they are used, and how
to create and run one.
Objectives
• Define what a script file is.
• Explain the step-by-step procedure for creating a script file in MATLAB.
• Demonstrate the procedure with a worked example (Circle Script).
• Apply the procedure to a practical exercise (Rectangle Script).
• Summarize the benefits of using script files over the command window.
1. What Is a Script File?
A script file is simply a collection of executable MATLAB® commands saved together in one file (an m-file).
• All pre-built commands used in MATLAB (plot, mean, std, exp, cosd, ...) are themselves script files or
functions.
• MATLAB allows the user to create customized m-files for specific applications or problems.
• A script file behaves exactly like typing each command sequentially at the command prompt — every
variable defined appears in the Workspace.
• Any line starting with % is a comment (turns green) and does not execute.
2. Procedure for Creating a Script File
1. Click New Script on the Home tab in the MATLAB Editor — this creates a blank script file.
2. Type the set of executable MATLAB commands in the editor window.
3. Save the file in an appropriate folder. The filename must follow the same rules MATLAB uses for naming
variables (start with a letter, no spaces or special characters).
4. Set the Current Directory in MATLAB to the same folder where the file was saved.
5. Run the script by clicking the Green Arrow (Run) in the editor, or by typing the filename at the command
prompt.
3. Worked Example — Circle Script
Task: calculate the Area and Circumference of a circle given its radius.
% Area and Circumference of a circle given its radius
radius = 5;
area = pi*radius^2;
circumference = 2*pi*radius;
disp("area =")
disp(area)
disp("circumference =")
disp(circumference)
Result (saved as CircleScript.m):
• area = 78.5398
• circumference = 31.4159
Note: after running, all variables (radius, area, circumference) appear in the Workspace. Typing clear then
CircleScript at the command prompt reproduces the exact same result.
4. Exercise — Rectangle Script
Task: for a rectangle of length 15.4 cm and width 10.8 cm, find the Area and Perimeter.
• Area = Length × Width
• Perimeter = 2(Length + Width)
% Area and Perimeter of a rectangle given its length and width
length = 15.4;
width = 10.8;
area = length*width;
perimeter = 2*(length+width);
disp("area =")
disp(area)
disp("perimeter =")
disp(perimeter)
Result (saved as RectangleScript.m):
• area = 166.32 cm²
• perimeter = 52.4 cm
5. Why Use Script Files?
Script files save time when running many commands. If an error is found in an early command that affects later
ones, fixing it in a script and re-running the whole file is far faster than re-typing each command individually at
the command prompt.
Key Takeaways
• A script file is a saved, reusable set of MATLAB commands.
• Filenames must follow MATLAB variable-naming rules.
• Current Directory must match the file's save location before running.
• Script files make debugging and repeated execution far more efficient.
Conclusion
Script files are a foundational tool in MATLAB, allowing engineering students to write, save, and re-execute sets
of commands efficiently. Mastering this concept is essential before progressing to functions and more advanced
MATLAB programming.
Reference
ASE 202 Course Material — Script Files (Lecture Slides), Department of Aerospace Engineering, FUTIA.
THANK YOU