0% found this document useful (0 votes)
32 views44 pages

CP Chapter 4 2021

The document discusses script files in MATLAB and managing data in the MATLAB workspace. Some key points: - A script file is a list of MATLAB commands saved in a file that can be executed. The MATLAB workspace contains variables defined during a session that can be accessed by script files and the command window. - The workspace window displays the variables in the workspace and allows them to be edited or deleted. Script files take input which can be defined and assigned values within the file or in the command window. - The disp and fprintf commands can be used to display output. Disp displays a variable while fprintf allows formatting and mixing of text and data. Fprintf can also save output to a file by opening

Uploaded by

Ahmed Ghaith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views44 pages

CP Chapter 4 2021

The document discusses script files in MATLAB and managing data in the MATLAB workspace. Some key points: - A script file is a list of MATLAB commands saved in a file that can be executed. The MATLAB workspace contains variables defined during a session that can be accessed by script files and the command window. - The workspace window displays the variables in the workspace and allows them to be edited or deleted. Script files take input which can be defined and assigned values within the file or in the command window. - The disp and fprintf commands can be used to display output. Disp displays a variable while fprintf allows formatting and mixing of text and data. Fprintf can also save output to a file by opening

Uploaded by

Ahmed Ghaith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Computer Programming

ECIV 2303

Chapter 4
Using Script Files and Managing Data

Civil Engineering Department


Prof. Mohammed Arafa
The Matlab Workspace And The Workspace Window

➢ A script file is a list of MATLAB commands, called a program,


that is saved in a file.
➢ The MATLAB workspace consists of the set of variables (named
arrays) that are defined and stored during a MATLAB session.
➢ It includes variables that have been defined in the Command
Window and variables defined when script files are executed.
➢ This means that the Command Window and script files share the
same memory zone within the computer.
The Matlab Workspace And The Workspace Window
The Matlab Workspace And The Workspace Window
The Matlab Workspace And The Workspace Window

✓ The elements in the Variable Editor Window can be edited.


✓ The variables in the Workspace Window can be deleted by selecting them,
and then either pressing the delete key on the keyboard or selecting delete
from the edit menu.
✓ This has the same effect as entering the command clear in the Command
Window.
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.
1. The variable is defined and assigned a value in the
script file.
2. The variable is defined and assigned a value in the
Command Window.
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
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.
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. (Recall that the variable is recognized
in the script file.)
✓ 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.
Input to a Script File
2- The variable is defined and assigned a value in the Command Window.

this script file is saved as


Chapter4Example3.m
Input to a Script File
2- The variable is defined and assigned a value in the Command Window.
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.

The form of the input command is:


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.
Input to a Script File
The input command can also be used to assign a string to a
variable. This can be done in one of two ways:
1. One way is to use the command in the same form as
shown above, and the string is typed between two single
quotes.
2. The second way is to use an option in the input command
that defines the characters that are entered as a string.
where the ‘s’ defines the characters that will be entered as
a string. In this case the text is typed in without the single
quotes, but it is assigned to the variable as a string.
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.
➢ The commands disp and fprintf can be used in the
Command Window, in a script file, and in a function file.
The disp Commands

The disp command is used to display the elements of a variable


without displaying the name of the variable, and to display text.
The format of the disp command is:
The disp Commands
The disp Commands
➢ Only one variable can be displayed in a disp command.
➢ If elements of two variables need to be displayed together, a
new variable (that contains the elements to be displayed)
must first be defined and then displayed.
➢ 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.
➢ Since in the disp command the user cannot control the
format (the width of the columns and the distance between
the columns) of the display of the array, the position of the
headings has to be aligned with the columns by adding
spaces.
The disp Commands
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.
➢ With many available options, the fprintf command can be long
and complicated. To avoid confusion, the command is presented
gradually as follows:
✓ How to use the command to display text messages.
✓ How to mix numerical data and text.
✓ How to format the display of numbers.
✓ How to save the output to a file.
The fprintf Command
Using the fprintf command to display text:

Example:
The fprintf Command

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:
The fprintf Command
The fprintf Command
Using the fprintf command to display a mix of text and numerical data:
The fprintf Command
The fprintf Command

➢ The field width and precision (5.2 in the previous example)


are optional.
➢ The first number (5 in the example) is the field width,
which specifies the minimum number of digits in the
display. If the number to be displayed is shorter than the
field width, spaces or zeros are added in front of the
number.
➢ The precision is the second number (2 in the example). It
specifies the number of digits to be displayed to the right of
the decimal point.
The fprintf Command

➢ The last element in the formatting elements, which is required, is the


conversion character, which specifies the notation in which the number is
displayed.
➢ Some of the common notations are:
e Exponential notation using lowercase e (e.g., 1.709098e+001).
E Exponential notation using uppercase E (e.g., 1.709098E+001).
f Fixed-point notation (e.g., 17.090980).
g The shorter of e or f notations.
G The shorter of E or f notations.
i Integer.
s Character vector or string array.
The fprintf Command
The fprintf Command
➢ 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 %f followed by any formatting
elements) at the places in the text where the numbers are to
be inserted.
➢ Then, after the string argument of the command (following
the comma), the names of the variables are typed in the order
in which they are inserted in the text. In general the
command looks like:
The fprintf Command
The fprintf Command

Additional remarks about the fprintf command:


➢ To place a single quotation mark in the displayed text, type two
single quotation marks in the string inside the 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.
The fprintf Command
The fprintf Command
Using the fprintf command to save output to a file:
➢ Writing output to a file requires three steps:
a) Opening a file using the fopen command.
b) Writing the output to the open file using the fprintf command.
c) Closing the file using the fclose command
The fprintf Command
Step a):
➢ Before data can be written to a file, the file must be opened. This is done
with the fopen command, which creates a new file or opens an existing
file. The fopen command has the form:

➢ 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:

➢ When either one of these commands is executed, all of the variables


currently in the workspace are saved in a file named file_name.mat that
is created in the current directory.
➢ In mat files, which are written in a binary format, each variable
preserves its name, type, size, and value. These files cannot be read by
other applications.
The save Commands
➢ The save command can also be used for saving only some of the
variables that are in the workspace.

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

➢ The name of the sheet ‘sheet_name’ is typed as a string.


➢ The ‘range’ (typed as a string) is a rectangular region of the spreadsheet
defined by the addresses (in Excel notation) of the cells at opposite
corners of the region. For example, ‘C2:E5’
Importing and exporting data into and from Excel:
➢ Exporting data from MATLAB to an Excel spreadsheet is done by using
the xlswrite command. The simplest form of the command is:

➢ ‘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.

You might also like