Writing Scripts and Basic Programming in MATLAB
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
file extension and executed to perform tasks or calculations. They do not accept inputs directly or return outputs like functions but are useful for automating tasks.
How to Create and Save a Script
- Open MATLAB and navigate to the Editor tab.
- Click on New Script to open a blank editor window.
- Write your commands in the editor.
- Save the script with a descriptive name using the
.m
extension, 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
.m
) in the Command Window and press Enter. - Click the Run button in the Editor toolbar.
- Use the
run
command in the Command Window followed by the script name, e.g.,run('example_script')
.
Where Are the Results Displayed?
The results of scripts are displayed in the Command Window. Use the disp
or fprintf
commands within your script to display variables or formatted output.
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);
To run this script, type basic_script
in the Command Window. The result will display as:
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 this script will display:
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
You can make your scripts interactive by using the input
function to get user input and disp
or fprintf
to display results.
% 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."]);
When run, the script prompts the user and displays:
Enter your name: Alice Enter your age: 25 Hello, Alice! You are 25 years old.
Another Matlab Interactive Script Example
Interactive scripts allow user input during execution, making them flexible for dynamic calculations or decision-making. Here’s an example of a script that calculates the area of a rectangle based on user-provided length and width.
% 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);
When you run this script, it will prompt you for input and display the result. For example:
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
Use the disp
command for displaying messages and the input
function for gathering user input during script execution.
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.