Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views10 pages

Script Files

Script files in MATLAB® are collections of executable commands that allow users to automate tasks and create custom functions. Users can create their own script files by typing commands in the editor, saving them in an appropriate folder, and running them to see results in the Workspace and Command Window. This document also includes activities for creating script files to calculate the area and circumference of a circle and the area and perimeter of a rectangle.

Uploaded by

greatgakaphimia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Script Files

Script files in MATLAB® are collections of executable commands that allow users to automate tasks and create custom functions. Users can create their own script files by typing commands in the editor, saving them in an appropriate folder, and running them to see results in the Workspace and Command Window. This document also includes activities for creating script files to calculate the area and circumference of a circle and the area and perimeter of a rectangle.

Uploaded by

greatgakaphimia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SCRIPT FILES

SCRIPT FILES

▪ All of the pre-built commands that you use in


MATLAB® are script files or functions (plot, mean, std,
exp, cosd, …)
▪ MATLAB® allows the user to create his/her own
customized m-files for specific applications or
problems.
▪ A script file is simply a collection of executable
MATLAB® commands. To create a new script file,
click on the New Script icon on the left side of the
Home Tab.
Click on
SCRIPT FILES Creates Blank
New Script Script File
SCRIPT FILE: PROCEDURE
1. Type a set of executable commands in the editor window.

2. Save the file in an appropriate folder. When you pick a


name for the file you must follow the same rules that
MATLAB has for naming variables.

3. Set the current directory in MATLAB® to the same place


where you saved your file.
ACTIVITY 1

• We will create a new script file and write


commands to calculate the Area and
Circumference of a Circle given its radius.
1. Create a new Folder on your desktop, and name it as your
Group number e.g. GROUP 1
2. Create a new script file in Matlab, save the filename as
CircleScript, and save in your Group folder.

3. Set the current directory in MATLAB to the same place


where you saved your file.
ACTIVITY 1 (CON’T)
▪ Type the commands shown below:
ACTIVITY 1: RUN THE SCRIPT FILE
• Remember to save the file as CircleScript in your newly created
folder.
▪ Now run your script file by clicking on the Green Arrow in the
m-file editor window. The result is shown below.
ACTIVITY 1: RESULT
• Note: Any line that starts with a % is a comment and turns green – it
doesn’t execute.
▪ Notice that every single variable defined in the script file (radius,
area, and circumference) appears in the Workspace Window. Area
and circumference are also displayed in the Command Window
because of the disp command.
▪ Clear the workspace window by typing clear at the command
prompt.
▪ At the command prompt, type the name of your script file: >>
CircleScript. Note, that the results are exactly the same as
before.
EXERCISE 1
▪ Create a new script file, name it as RectangleScript and
save it in your Group folder.
▪ The Area and Perimeter of a Rectangle is given by
▪ 𝐴𝑟𝑒𝑎 = 𝐿𝑒𝑛𝑔𝑡ℎ ∗ 𝑊𝑖𝑑𝑡ℎ
▪ 𝑃𝑒𝑟𝑖𝑚𝑒𝑡𝑒𝑟 = 2(𝐿𝑒𝑛𝑔𝑡ℎ + 𝑊𝑖𝑑𝑡ℎ)
▪ For a rectangle having a length and width of 15.4cm and 10.8cm
respectively, write the matlab commands to find the area and
perimeter of the rectangle, and display the result for both on the
Command window.
SUMMARY ON SCRIPT FILES
▪ A script file is simply a set of executable MATLAB commands saved
together in a file. It behaves exactly the same as entering each
command sequentially in the command window. Every single
variable defined appears in the workspace.
▪ Script files can be really useful if you are going to execute a lot of
MATLAB commands. For example, suppose you execute 15
commands at the command prompt and discover an error in the first
command that affected the last 14 commands. In the command
window, you would have to fix the error in the first command then run
the other 14 commands over again. If these commands were in a
script file, you could fix the first command and re-run the script file –
much faster !!

You might also like