Writing Scripts and Basic Programming in MATLAB Get Certified
MATLAB scripts are a fundamental aspect of automating computations, organizing code, and creating reusable workflows. This section introduces the basics of writing scripts, running them, and programming in MATLAB.
What is a Script?
A script in MATLAB is a plain-text file that contains a sequence of commands. Scripts are saved with a .m extension and executed to perform tasks or calculations. Unlike functions they do not accept formal inputs or return formal outputs, yet they are ideal for automating repetitive work.
How to Create and Save a Script
- Open MATLAB and navigate to the Editor tab.
- Click New Script to open a blank editor window.
- Write your commands in the editor.
- Save the script with a descriptive name using the
.mextension (e.g.,example_script.m).
How to Run a Script
There are several ways to run a MATLAB script:
- Type the script’s name (without the
.m) in the Command Window and press Enter. - Click the Run button in the Editor toolbar.
- Use the
runcommand, e.g.run('example_script').
Where Are the Results Displayed?
Results appear in the Command Window. Use disp or fprintf inside the script to show variables or formatted text.
Basic Script Example
% Save this script as basic_script.m
a = 5;
b = 10;
sum = a + b;
disp("The sum of a and b is:");
disp(sum);
Running the script produces:
The sum of a and b is: 15
Advanced Script Example
% Save this script as advanced_script.m
% Script to calculate and display the area of a circle
radius = 5;
area = pi * radius^2;
disp("The radius of the circle is:");
disp(radius);
disp("The area of the circle is:");
disp(area);
Running the script produces:
The radius of the circle is: 5 The area of the circle is: 78.5398
Control Structures in Scripts
Conditional Statements
% Save this as condition_script.m
x = -3;
if x > 0
disp("x is positive");
elseif x == 0
disp("x is zero");
else
disp("x is negative");
end
Loops in Scripts
% Save this as loop_script.m
% For loop to print numbers 1 to 5
for i = 1:5
disp(["Iteration:", i]);
end
% While loop example
count = 1;
while count <= 3
disp(["Count:", count]);
count = count + 1;
end
Using Input and Output in Scripts
Scripts can be made interactive with the input function and can display results with disp or fprintf.
% Save this script as interactive_script.m
name = input("Enter your name: ", 's');
age = input("Enter your age: ");
disp(["Hello,", name, "! You are", age, "years old."]);
Example interaction:
Enter your name: Alice Enter your age: 25 Hello, Alice! You are 25 years old.
Another Interactive Script Example
Interactive scripts allow user input during execution, making them flexible for dynamic calculations. The following script computes the area of a rectangle from user-supplied dimensions.
% Save this script as interactive_rectangle_area.m
% Script to calculate the area of a rectangle interactively
disp("This script calculates the area of a rectangle.");
length = input("Enter the length of the rectangle: ");
width = input("Enter the width of the rectangle: ");
area = length * width;
disp("The area of the rectangle is:");
disp(area);
Example interaction:
This script calculates the area of a rectangle. Enter the length of the rectangle: 10 Enter the width of the rectangle: 5 The area of the rectangle is: 50
Useful MATLAB Commands for Scripts
Practice Questions
Test Yourself
1. Write a script to calculate the perimeter of a rectangle given its length and width.
2. Create a script to find the factorial of a user-provided number.
3. Write a script to generate and plot a cosine wave.
4. Use a loop to calculate the sum of the first 50 natural numbers.