Professional Documents
Culture Documents
Edit Window
Function M-file
Script M-file
1.1.2 Script M-files and function M-files
Commands can be entered in command window, or stored
in a script M-file or a function M-file. Using the command
window for calculations is an easy and powerful tool.
However once you close the MATLAB program, all your
calculations are lost. To avoid loss of programs, the user
can create and save code in the so called M-files. Once
saved, these files can be reused by the programmer.
A script M-file can call a function M-file to perform certain
calculations and then return command to the script file.
Using Command window only:
>> x = 10;
>> area = pi*x^2
Using Command window and function M-
file
In the edit window create a function as follows:
function f = circle(x)
f = pi*x^2
End
Notation:
f is the output
circle is the function name
x is input argument (a scalar or vector).
The MATLAB input argument (x) is used to pass data between the function and command window or a script file or
another function.
The user must save the function in a folder with an identical name to the function name. Therefore save the
function under the name circle.m.
Then in the command window type:
>> x = 10;
>> circle(x)
Using a script file only:
Go to the edit window, create and save a script file with the
name test1.m
x = 10;
area = pi*x^2;
x = 10;
circle(x)