CP Chapter 4 2021
CP Chapter 4 2021
ECIV 2303
Chapter 4
Using Script Files and Managing Data
Example:
The fprintf Command
➢ fid is a variable called the file identifier. A scalar value is assigned to fid
when fopen is executed. The file name is written (including its
extension) within single quotes as a string.
The fprintf Command
➢ The permission is a code that tells how the file is opened. Some of the
more common permission codes are:
‘r’ Open file for reading (default).
‘w’ Open file for writing. If the file already exists, its content is
deleted. If the file does not exist, a new file is created.
‘a’ Same as ‘w’, except that if the file exists the written data is
appended to the end of the file.
‘r+’ Open (do not create) file for reading and writing.
‘w+’ Open file for reading and writing. If the file already exists, its
content is deleted. If the file does not exist, a new file is created.
➢ ‘a+’ same as ‘w+’, except that if the file exists the written data is
appended to the end of the file.
➢ If a permission code is not included in the command, the file opens with
the default code ‘r’.
The fprintf Command
Step b):
➢ Once the file is open, the fprintf command can be used to write output
to 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. The fprintf command then
has the form:
The fprintf Command
Step c):
➢ When the writing of data to the file is complete, the file is closed using
the fclose command. The fclose command has the form:
Additional notes on using the fprintf command for saving output to a file:
➢ The created file is saved in the current directory.
➢ It is possible to use the fprintf command to write to several different
files. This is done by first opening the files, assigning a different fid to
each (e.g. fid1, fid2, fid3, etc.), and then using the fid of a specific file in
the fprintf command to write to that file.
The fprintf Command
The fprintf Command
➢ When the script file above is executed two new .txt files, named
VmphtoVkm and FlbtoFN, are created and saved in the current directory.
➢ These files can be opened with any application that can read .txt files.
The save Commands
➢ The save command is used for saving the variables (all or some of
them) that are stored in the workspace. The two simplest forms of the
save command are:
➢ 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 (for example, save file_name -ascii).
➢ In the ASCII format the variable’s name, type, and size are not
preserved. The data is saved as characters separated by spaces but
without the variable names.
The load Commands
➢ The load command can be used for retrieving variables that were saved
with the save command back to the workspace
➢ When the command is executed, all the variables in the file (with the
name, type, size, and values as were saved) are loaded back to the
workspace.
➢ The load command can also be used for retrieving only some of the
variables that are in the saved .mat file.
Importing and exporting data into and from Excel:
➢ 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:
➢ ‘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.
Importing and exporting data into and from Excel:
➢ When an Excel file has several sheets, the xlsread command can be
used to import data from a specified sheet or import only a portion of
the data that is in the spreadsheet.
➢ ‘filename’ (typed as a string) is the name of the Excel file to which the
data is exported.
➢ 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.