Script and Function Files
Script and Function Files
• A script file can be executed directly from the Editor Window by clicking on
the Run icon
• It can also be executed typing the file name in the Command Window and
then pressing the Enter key.
• The file will be executed if the folder where the file is saved is the current
folder of MATLAB
• The user can also change the current folder to the folder where the script
file is saved
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Input into a script file
• When a script file is executed, the variables that are used in the
calculations within the file must have assigned values
Variables specified
File name
data
File name
data
example gives:
example gives:
example gives:
example gives:
The form of the function definition line is: function [output arguments] = function_name(input arguments)
• The function body contains the computer program (code) that actually
performs the computations
• When more than one function file is created, each of them has its own
variables and do not share with other function files
• This is done by declaring the variable global with the global command
• In this way the function is called (used) by using the function name.
• Both script and function files are saved with the extension .m
• The first executable line in a function file is (must be) the function
definition line.
• The variables in a function file are local. The variables in a script file
are recognized in the Command Window.
• Script files can use variables that have been defined in the workspace.
• Script files contain a sequence of MATLAB commands (statements).
• Function files can accept data through input arguments and can return
data through output arguments.