0% found this document useful (0 votes)
71 views9 pages

Script Files

A script file is a collection of executable MATLAB commands saved in a file with a .m extension. It allows users to run pre-programmed commands by running the script file. When run, a script file executes each command sequentially and displays all output and variables in the workspace, just as if the commands were manually entered in the command window. Script files make it easier to rerun analyses without retyping commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views9 pages

Script Files

A script file is a collection of executable MATLAB commands saved in a file with a .m extension. It allows users to run pre-programmed commands by running the script file. When run, a script file executes each command sequentially and displays all output and variables in the workspace, just as if the commands were manually entered in the command window. Script files make it easier to rerun analyses without retyping commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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 New
Script
Script Files Creates Blank
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 the script file.
4. To run the script file: Hit the Green Run Arrow in the Editor
window or simply type the name of the file (without the .m
extension) at the command prompt in the MATLAB
command window.

The file must be in your current directory or


it will not run!
Exercise 1: New Script File
 Right click in the current folder window in MATLAB and
create a new folder named whatever you would like.
 Double click on the folder to make it your current
folder.
 Clear your MATLAB workspace by typing clear at
the command prompt.
 Click on New Script to open a blank script file.
 Type the commands on the next slide into the editor
window then save the file as CircleScript in your newly
created folder.
Exercise 1: Script File

Save the file as CircleScript in your newly created folder.

Note: Any line that starts with a % is a comment and


turns green – it doesn’t execute.
Exercise 1: Run the Script File
 Now run your script file by clicking on the Green Arrow
in the m-file editor window.
 Notice that every single variable defined in the script file
(radius, area, and circum) appears in the Workspace
Window. Area and circum 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.
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 !!
Exercise 2: Another Script File
 Create a new script file called SphereScript that will
compute and display the surface area and the volume
of a sphere.

You might also like