Basic
Computer
Programming
Dr. Deniz Kutluay
Using Script Files and
Managing Data
• THE MATLAB WORKSPACE
• INPUT TO A SCRIPT FILE
• OUTPUT COMMANDS
• THE save AND load COMMANDS
• IMPORTING AND EXPORTING DATA
Mission & Goals
Mission
Listen to me carefully, and practice some examples using Matlab by following me
Goal #1 Goal #2 Goal #3
To learn how to To learn how to To learn how to
insert variables use commands import and export
into a script file. save and load for numerical data
saving and from files.
retrieving data.
Basic Computer Programming Dr. Deniz Kutluay
The Matlab workspace
A script file is a list of MATLAB commands,
called a program, that is saved in a file.
When the script file is executed (run),
MATLAB executes the commands.
The MATLAB workspace consists of the set
of variables (named arrays) that are
defined and stored during a MATLAB
session
➢ Command Window and script files share
the same memory zone within the
computer. 001
Basic Computer Programming Dr. Deniz Kutluay
The Matlab workspace
The variables currently in memory can
also be viewed in the Workspace Window.
The variables that are displayed in the
Workspace Window can also be edited.
Double-clicking on a variable opens the
Variable Editor Window.
The elements in the Variable Editor
Window can be edited.
002
Basic Computer Programming Dr. Deniz Kutluay
Input to a script file
➢ When a script file is executed, the variables that are used in the calculations within
the file must have assigned values.
➢ The assignment of a value to a variable can be done in three ways, depending on
where and how the variable is defined.
003
Basic Computer Programming Dr. Deniz Kutluay
Input to 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.
004
Basic Computer Programming Dr. Deniz Kutluay
Input to 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.
005
Basic Computer Programming Dr. Deniz Kutluay
Input to 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.
006
Basic Computer Programming Dr. Deniz Kutluay
Input to 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.
▪ When the input command is executed
as the script file runs, the string is
displayed in the Command Window.
▪ The string is a message prompting the
user to enter a value that is assigned to
the variable.
▪ The user types the value and presses
the Enter key. This assigns the value to
the variable.
007
Basic Computer Programming Dr. Deniz Kutluay
Output commands
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.
1. The disp Command
The disp command is used to display the
elements of a variable without displaying
the name of the variable, and to display
text. 008
Basic Computer Programming Dr. Deniz Kutluay
Output commands
1. The disp Command
In many situations it is nice to display
output (numbers) in a table.
This can be done by first defining a
variable that is an array with the numbers
and then using the disp command to
display the array.
009
Basic Computer Programming Dr. Deniz Kutluay
Output commands
2. 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.
Using the fprintf command to display text:
010
Basic Computer Programming Dr. Deniz Kutluay
Output commands
2. The fprintf Command
Using the fprintf command to display a
mix of text and numerical data:
The field width and precision are optional.
The first number (5 in the example) is the
field width, which specifies the minimum
number of digits in the display.
The precision (2 in the example) specifies
the number of digits to be displayed
to the right of the decimal point. 011
Basic Computer Programming Dr. Deniz Kutluay
Output commands
2. The fprintf Command
Using the fprintf command to display a
mix of text and numerical data:
The last element in the formatting
elements, which is required, is the
conversion character, which specifies
the notation in which the number is
displayed.
012
Basic Computer Programming Dr. Deniz Kutluay
Output commands
2. The fprintf Command
Using the fprintf command to display a
mix of text and numerical data:
With the fprintf command it is possible
to insert more than one number
(value of a variable) within the text.
This is done by typing %g (or % followed
by any formatting elements) at the
places in the text where the numbers
are to be inserted.
013
Basic Computer Programming Dr. Deniz Kutluay
Output commands
Additional remarks about the fprintf
command:
The fprintf command is vectorized.
This means that when a variable that is
a vector or a matrix is included in the
command, the command repeats itself
until all the elements are displayed.
If the variable is a matrix, the data is used
column by column.
014
Basic Computer Programming Dr. Deniz Kutluay
Output commands
Using the fprintf command to save output
to a file:
The fprintf command can be used for
writing the output to a file when it is
necessary to save the output.
The data that is saved can subsequently
be displayed or used in MATLAB
and in other applications.
015
Basic Computer Programming Dr. Deniz Kutluay
Output commands
Step (a):
Before data can be written to a file, the
file must be opened. This is done with the
fopen command:
fid is a variable called the file identifier. A
scalar value is assigned to f id when
fopen is executed.
The permission is a code that tells how
the file is opened. 016
Basic Computer Programming Dr. Deniz Kutluay
Output commands
Step (b): Step (c):
Once the file is open, the fprintf When the writing of data to the file is complete, the file
command can be used to write output to is closed using the fclose command.
the file.
The fprintf command is used in exactly
the same way as it is used to display
output in the Command Window, except
that the variable fid is inserted inside the
command.
017
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
▪ The save and load commands are most useful for saving and retrieving data
for use in MATLAB.
▪ The save command can be used for saving the variables that are currently
in the workspace, and the load command is used for retrieving variables
that have been previously saved, to the workspace.
▪ The workspace can be saved when MATLAB is used in one type of platform
(e.g.,PC), and retrieved for use in MATLAB in another platform (e.g., Mac).
▪ The save and load commands can also be used for exchanging data with
applications outside MATLAB. 018
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The save command
The save command is used for saving the
variables (all or some of them) that are
stored in the workspace.
When either one of these commands is In mat files, which are written in a binary format,
executed, all of the variables currently in each variable preserves its name, type, size, and
the workspace are saved in a file named value.
file_name.mat
019
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The save command
The save command can also be used for
saving only some of the variables that are
in the workspace.
020
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The save command
The save command can also be used for
saving in ASCII format, which can be read
by applications outside MATLAB.
Saving in ASCII format is done by adding
the argument -ascii in the command.
In the ASCII format the variable's name,
type, and size are not preserved.
The data is saved as characters separated
021
by spaces but without the variable names.
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The load command
The load command can be used for
retrieving variables that were saved with
the save command back to the
workspace, and for importing data that The load command can also be used for retrieving
was created with other applications and only some of the variables that are in the saved
saved in ASCII format or in text (.txt) files. .mat file.
When the command is executed, all the
variables in the file (with the name, type,
size, and values as were saved) are added
(loaded back) to the workspace.
022
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The load command
The load command can also be used to
import data that is saved in ASCII
or text (.txt) to the workspace.
When data is loaded from an ASCII or text
file into the workspace, it has to be
assigned to a variable name.
If the data is in a text file, the extension
.txt has to be added to the file name.
023
Basic Computer Programming Dr. Deniz Kutluay
The save and load
commands
The load command
The data shown in figure (a 3 x 2 matrix) is
typed in Notepad, and then saved as
DataFromText.txt
In the first command the data is assigned
to a variable named DfT.
In the second command the data is
automatically assigned to a variable
named DataFromText, which is the name
of the text file where the data was saved.
024
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
This section describes only how to import
and export numerical data
Commands for Importing and Exporting
Data
This section describes how to transfer
data into and out of Excel spreadsheets.
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. 025
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
• 'filename' (typed as a string) is the name
of the Excel file. The directory of the Excel
file must be either the current directory or
listed in the search path.
• If the Excel file has more than one sheet,
the data will be imported from the first
sheet.
026
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
When an Excel file has several sheets, the
xlsread command can be used to import
data from a specified sheet.
027
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
Another option is to import only a portion
of the data that is in the spreadsheet.
The 'range' is a rectangular region of the
spreadsheet defined by the addresses (in
Excel notation) of the cells at opposite
comers of the region. For example, 'C2:E5'
is a 4 x 3 region of rows 2, 3, 4, and 5 and
columns C, D, and E.
028
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
Exporting data from MATLAB to an Excel
spreadsheet is done by using the xlswrite
command.
• 'filename' is the name of the Excel file to
which the data is exported. The file must
be in the current directory. If the file does
not exist, a new Excel file with the
specified name will be created.
029
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
• variable_name is the name of the
variable in MATLAB with the assigned
data that is being exported.
• The arguments 'sheet_name' and 'range'
can be added to the xlswrite command to
export to a specified sheet and to a
specified range of cells, respectively.
030
Basic Computer Programming Dr. Deniz Kutluay
Importing and
exporting data
Commands for Importing and Exporting
Data
The data from the Excel spreadsheet
shown in Figure is imported into MATLAB
by using the xlsread command.
The spreadsheet is saved in a file named
TestData1 in the current directory.
Then the data is imported into MATLAB
by assigning it to the variable DATA:
031
Thank
You
Dr. Deniz Kutluay
Electrical-Electronics Engineering
Design Architect at Vestel
deniz.kutluay@vestel.com.tr
deniz.kutluay@cbu.edu.tr