0% found this document useful (0 votes)
8 views

Script and Function Files

The document discusses the creation and use of script and function files in MATLAB, emphasizing the convenience of executing a series of commands through script files rather than the Command Window. It explains how to create, save, and execute script files, as well as how to define user-defined functions with local and global variables. Additionally, it covers input and output commands, including importing data from Excel and displaying results using disp and fprintf commands.

Uploaded by

6bg244zsxb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Script and Function Files

The document discusses the creation and use of script and function files in MATLAB, emphasizing the convenience of executing a series of commands through script files rather than the Command Window. It explains how to create, save, and execute script files, as well as how to define user-defined functions with local and global variables. Additionally, it covers input and output commands, including importing data from Excel and displaying results using disp and fprintf commands.

Uploaded by

6bg244zsxb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Script and function files

Jude Kwaku Bonsu (PhD)

Department of Chemical Engineering, Kwame Nkrumah University of Science and Technology


Why do we need a script file?

• So far all the commands were typed in the Command Window

• using the Command Window to execute a series of commands—


• especially if they are related to each other (a program)—is not
convenient

• It may prove difficult or even impossible

• A better way of executing commands with MATLAB is first to


• create a file with a list of commands, save it, and then execute
• the file Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
More on script files

• A script file is a sequence of MATLAB commands, also called a program


• When a script file runs (is executed), MATLAB executes the commands
in the order they are written
• Script files can be typed and edited in any text editor and then pasted
into the MATLAB editor.
• Script files are also called M-files because the extension .m is used
when they are saved
• In MATLAB script files are created and edited in the Editor/Debugger
Window
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
The Editor window

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Creating a script file

Comments created to guide user

Variables being created

Pressure being calculated

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
More on script files
• Before a script file can be executed it has to be saved

• 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

• i.e. the variables must be in the workspace

• The assignment of a value to a variable can be done in three ways,


depending on where and how the variable is defined

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Input into a script file
1. The variable is defined and assigned a value in the script file.
• In this case the assignment of a value to the variable is part of the
script file.
• If the user wants to run the file with a different variable value…
• the file must be edited and the assignment of the variable
changed.
• Then, after the file is saved, it can be executed again.

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Creating a script file

Variables specified

Pressure being calculated

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Input into a script file
2. The variable is defined and assigned a value in the Command
Window.
• In this case the assignment of a value to the variable is done in the
Command Window.
• If the user wants to run the script file with a different value for the
variable
• the new value is assigned in the Command Window and the file is
executed again.

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Creating vector variables

Variables are declared, before


New_script is called in the command
window
Square brackets are optional

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Input into a script file
3. The variable is defined in the script file, but a specific value is
entered in the Command Window when the script file is executed.
In this case the variable is defined in the script file, and when the
file is executed,
the user is prompted to assign a value to the variable in the
Command Window.
This is done by using the input command for creating the variable.
The form of the input command is
variable_name = input(‘message that is displayed in the Command Window’)
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Script taking inlet in command window

6 elements to be created between 1


and 12

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Script taking inlet in command window

6 elements to be created between 1


and 12

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Importing data from excel
• There are times users of MATLAB will be using external data for
analysis
• Hence will need to import data

• Importing data from Excel is done with the xlsread command.


When the command is executed, the data from the spreadsheet is
assigned as an array to a variable.

• The simplest form of the xlsread command is:


• variable_name = xlsread(‘filename’)

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
More on importing data from Excel
• When an Excel file has several sheets, the xlsread command can
be used to import data from a specified sheet.
• variable_name = xlsread(‘filename’,‘sheet_name’)
• Another option is to import only a portion of the data that is in
the spreadsheet This is done by typing an additional argument in
the command
• variable_name = xlsread(‘filename’,‘sheet_name’,‘range’)

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Importing data from excel

File name

data

Department of Chemical Engineering


More on importing data from excel

File name

data

Department of Chemical Engineering


Output commands
• MATLAB has several commands that can be used to generate
displays
• The displays can be messages that provide information, numerical
• data, and plots.
• Two commands that are frequently used to generate output are
disp and fprintf
• The disp command displays the output on the screen, while the
fprintf command can be used to display the output on the screen
or to save the output to a file.
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
The disp command
• The disp command is used to display the elements of a variable
without displaying the name of the variable

• It can also be used to display text

• The format of the disp command is: disp(name of a variable) or


disp(‘text as string’)
• It can be used both in the command window and as part of a script
• Now we will use the disp command to modify our pressure calculating
code

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Pressure computing software

Department of Chemical Engineering


Pressure computing software (command window)

Department of Chemical Engineering


The fprintf command
• The fprintf command can be used to display output (text and data) on
the screen or to save it to a file
• With this command (unlike with the disp command)the output can be
formatted
• For example, text and numerical values of variables can be intermixed
and displayed in the same line.
• In addition, the format of the numbers can be controlled.

• The various aspects of the fprintf will be presented gradually

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text
• To display text, the fprintf command has the form:

• fprintf(‘type text in a string’)

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text
• With the fprintf command it is
possible to start a new line in the
middle of the string.
• This is done by inserting \n before
the character that will start the
• new line.
• For example, inserting \n after the
first sentence in the previous
example gives:

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display a mix of text and numerical data

• To display a mix of text and a number (value of a variable), the fprintf


command has the form:
• fprintf(‘text as string %-5.2f additional text’,variable_name)

• With the fprintf command it is possible to insert more than one


number (value of a variable) within the text.
• The command has the form:
• fprintf(‘..text...%g...%g...%f...’,variable1,variable2,variable3)

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text and numeric data

• With the fprintf command it is


possible to start a new line in the
middle of the string.
• This is done by inserting \n before
the character that will start the
• new line.
• For example, inserting \n after the
first sentence in the 4previous
significant figures 1 decimal place

example gives:

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text and numeric data

• With the fprintf command it is


possible to start a new line in the
middle of the string.
• This is done by inserting \n before
the character that will start the
• new line.
• For example, inserting \n after the
first sentence in the 4previous
significant figures 1 decimal place

example gives:

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text and numeric data

• With the fprintf command it is


possible to start a new line in the
middle of the string.
• This is done by inserting \n before
the character that will start the
• new line.
• For example, inserting \n after the
first sentence in the 4previous
significant figures 1 decimal place

example gives:

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using the fprintf command to display text and numeric data

• With the fprintf command it is


possible to start a new line in the
middle of the string.
• This is done by inserting \n before
the character that will start the
• new line.
• For example, inserting \n after the
first sentence in the 4previous
significant figures 1 decimal place

example gives:

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
fprintf

• You can learn more about the fprintf command at


www.Mathworks.com

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
User defined functions and function files

• A user-defined function is a MATLAB program that is created by the


user, saved as a function file
• The function can be a simple single mathematical expression or a
complicated and involved series of calculations
• Function files are created and edited, like script files, in the
Editor/Debugger Window
• The figure below describes the basic features of a function file

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
More on function files

• The first line is the function definition line, which is followed by


comments the describe the function.
• This is followed by comments that describe the function

• Next comes the program

• the last line is an end statement, which is optional

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
The structure of a function file (The function defining line)

• The first executable line in a function file must be the function


definition line. Otherwise the file is considered a script file
• The purpose of a function defining line are :

1. Defines the file as a function file


2. Defines the name of the function
3. Defines the number and order of the input and output arguments

The form of the function definition line is: function [output arguments] = function_name(input arguments)

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
The structure of a function file (The function body)

• The function body contains the computer program (code) that actually
performs the computations

• The code can use all MATLAB programming features.

• This includes calculations, assignments, any built-in or user-defined


functions, logical statements

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
More on a function file (Local and global variables)

• All the variables in a function file are local


• The input and output arguments and any variables that are assigned
values within the function file.
• This means that the variables are defined and recognized only inside
the function file
• MATLAB uses a memory separate from the workspace when executing
a function file
• this means that a function file can have variables with the same names
as variables in the Command Window or in script files

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
More on a function file (Local and Global variables)

• When more than one function file is created, each of them has its own
variables and do not share with other function files

• It is possible, however, to make a variable common (recognized) in


several different function files, and perhaps in the workspace too

• This is done by declaring the variable global with the global command

• For example global Universal_Gas_CONST


Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Saving a function file

• A function file must be saved before it can be used. This is done, as


with a script
• file, by choosing Save as . . . from the File menu, selecting a location,
and entering the file name.

• It is highly recommended that the file be saved with a name that is


identical to the function name in the function definition line.

• In this way the function is called (used) by using the function name.

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Using a user-defined function
• A user-defined function is used in the same way as a built-in function.
• The function can be called from the Command Window, from a script file, or from another function.
• To use the function file, the folder where it is saved must either be in the current folder or be in the search path

Functions as a standalone file

Functions as part of a script


Using a user define function

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology
Comparing script and function files

• 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.

Department of Chemical Engineering, Kwame


Nkrumah University of Science and Technology

You might also like