Lesson: Matlab Code (Creating, Saving,
and Executing a Script File)
to create Script files (MATLAB code) and execute them.
A Script File
is a user-created file
with a sequence of MATLAB
in it. You're actually creating MATLAB code, here. The file must be
saved with a '.m'
extension, thereby, making it an m-file.
The code is executed by typing its file name (without the '.m'
extension') at the command
Now you'll write a script file to draw the unit circle of Tutorial
You are essentially going to write the same commands in a file, save
name it, and execute it within MATLAB.
Follow these directions:
Create a new file. On PCs, select 'New' -> 'M-File' from the
or use the related icons.
A new edit window
Type the following lines into this window. Lines starting with a '%'
interpreted as comments and
are ignored by MATLAB, but are very
useful for you, because then you can explain the meaning of the
% CIRCLE - A
script file to draw a pretty circle
angle = linspace(0, 2*pi, 360);
x = cos(angle);
y = sin(angle);
Write and save the file under the name 'prettycircle.m'.
On PCs select 'Save As...' from the File menu. A dialog box appears.
Type the name of the document as prettycircle.m.
Make sure the file is
being saved in the folder you want it to be in (the current working
folder / directory of MATLAB).
Click on the 'Save'
icon to save the file.
Now go back to the MATLAB command
window and type the following command to execute the
And you achieve the same 2D plot that you achieved in Tutorial
3, but the difference is that you saved all the instructions
in a file
that can be accessed and run by other m-files! It's like
'Matlab Code' to home
Code' to 'Matlab Tutorial'