Importing and Exporting Data: Lab Session 07
Importing and Exporting Data: Lab Session 07
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.
1
• If the Excel file has more than one sheet, the data will be imported from the first sheet.
When an Excel file has several sheets, the xlsread command can be used to import data
from a specified sheet. The form of the command is then:
• The name of the sheet is typed as a string. 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:
• The 'range 1 (typed as a string) 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: ES 1 is a 4 x 3 region of rows 2, 3, 4, and 5 and columns C, D, and E.
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.
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.
• 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. Using the import Wizard
Using the Import Wizard is probably the easiest way to import data into MATLAB since
the user does not have to know, or to specify, the format of the data. The Import Wizard is activated
by selecting Import Data in the File menu of the Command Window. (It can also be started by
typing the command uiimport.) The Import Wizard starts by displaying a file selection box that
shows all the data files recognized by the Wizard. The user then selects the file that contains the
data to be imported, and clicks Open. The Import Wizard opens the file and displays a portion of
the data in a preview box so that the user can verify that the data is the correct choice. The Import
Wizard tries to process the data, and if the wizard is successful, it displays the variables it has
2
created with a portion of the data. The user clicks next and the wizard shows the Column Separator
that was used. If the variable has the correct data, the user can proceed with the wizard (click next);
otherwise the user can choose a different Column Separator. In the next window the wizard shows
the name and size of the variable to be created in MATLAB. (When the data is all numerical, the
variable in MATLAB has the same name as the file from which the data was imported.) When the
wizard ends (click finish), the data is imported to MA TLAB.
Two-Dimensional Plots
Plots are a very useful tool for presenting information. This is true in any field, but
especially in science and engineering, where MATLAB is mostly used. MATLAB has many
commands that can be used for creating different types of plots. These include standard plots with
linear axes, plots with logarithmic and semi-logarithmic axes, bar and stairs plots, polar plots,
three-dimensional contour surface and mesh plots, and many more.
The arguments x and y are each a vector (one-dimensional array). The two vectors must have the
same number of elements. When the plot command is executed, a figure is created in the Figure
Window. If not already open, the Figure Window opens automatically when the command is
executed. The figure has a single curve with the x values on the abscissa (horizontal axis) and they
values on the ordinate (vertical axis). The curve is constructed of straight-line segments that
connect the points whose coordinates are defined by the elements of the vectors x and y. Each of
the vectors, of course, can have any name. The vector that is typed first in the plot command is
used for the horizontal axis, and the vector that is typed second is used for the vertical axis. If only
one vector is entered as an input argument in the plot command (for example plot (y)) than the
figure will show a plot of the values of the elements of the vector ( y(l), y(2), y(3), ... ) versus the
element number ( 1, 2, 3, ... ).
3
The plot command has additional, optional arguments that can be used to specify the color
and style of the line and the color and type of markers, if any are desired. With these options the
command has the form:
Line Specifiers
Line specifiers are optional and can be used to define the style and color of the line and the
type of markers (if markers are desired). The line style specifiers are:
4
• The specifiers are optional. This means that none, one, two, or all three types can be
included in a command.
Some examples are:
Properties are optional and can be used to specify the thickness of the line, the size of the
marker, and the colors of the marker's edge line and fill. The Property Name is typed as a string,
followed by a comma and a value for the property, all inside the plot command.
Four properties and their possible values are:
The three line specifiers, which indicate the style and color of the line, and the type of the
marker can also be assigned with a PropertyName argument followed by a PropertyValue
argument. The Property Names for the line specifiers are:
5
As with any command, the plot command can be typed in the Command Window, or it can
be included in a script file. It also can be used in a function file. It should also be remembered that
before the plot command can be executed, the vectors x and y must have assigned elements. This
can be done by entering values directly, by using commands, or as the result of mathematical
operations. The next two subsections show examples of creating simple plots.
Plot of a Function
In many situations there is a need to plot a given function. This can be done in MATLAB
by using the plot or the fplot command. The use of the plot command is explained below. The fplot
command is explained in detail in the next section.
In order to plot a function y = f(x) with the plot command, the user needs to first create a
vector of values of x for the domain over which the function will be plotted. Then a vector y is
created with the corresponding values of f(x) by using element-by-element calculations. Once the
two vectors are defined, they can be used in the plot command. The fplot Command
The fplot command plots a function with the form y = f(x) between specified limits. The command
has the form:
• The function can be typed directly as a string inside the command. The functions can
include MATLAB built-in functions and functions that are created by the user.
• The function to be plotted can be typed as a function of any letter.
• The function cannot include previously defined variables.
6
• The limits argument is a vector with two elements that specify the domain of x [xmin,
xmax], or a vector with four elements that specifies the domain of x and the limits of the
yaxis [xmin, xmax, ymin, ymax].
• The line specifiers are the same as in the plot command.
Practice Exercise
1. Plot the function
Figure 1
2. Make a plot of a circle with its center at (4.2, 2.7) and radius of 7.5.
7
Figure 2
3. Two parametric equations are given by:
In one figure, make plots of y versus x and v versus u for 0≤ t ≤2π. Format the plot such that the
both axes will range from -2 to 2.