2.10. Importing Data Into MATLAB¶
Data files having several formats such as TXT, CSV, XLS, and XLSX can be imported into MATLAB both interactively and from commands.
See the list of MATLAB supported import and export file formats
This video covers importing spreadsheet data into MATLAB.
This more detailed video covers importing data. It also covers some other cool MATLAB features.
2.10.1. Saving and Loading Workspace Data¶
You can save data from the workspace and load data into the workspace using
the save
and load
commands. These commands use files with a ‘.mat’
file name extension that are only used with MATLAB.
>> save myData
Creates myData.mat containing current workspace variables.
>> save someFile x y
Instead of saving the entire MATLAB workspace, you can selectively save individual variables. Saves x and y to someFile.mat.
>> load myData
Loads variables from myData.mat into current workspace.
2.10.2. Import Tool¶
Use of the Import Tool will be demonstrated and discussed in class. It is available from the Home tab, labeled as “Import Data”. Notice the pull down menu specifying “Output Type”. The choice of “Column vectors” yields similar vector data as we have previously used. Each column of data from the file is imported as a column vector.
Notice on the pull down menu on the right side of the Import Tool labeled “Import Selection”. The two choices “Generate Script” and “Generate Function” could be useful to automate the task for future use. Importing the data as column vectors is one of the complex import tasks, so the Import Tool helps out quite a bit.
2.10.3. Reading Tables¶
Column vectors is what we are most familiar with at this point, but it may not always be the best choice. Importing data as a table is simpler to perform from commands and has the advantage of keeping the rows of the data together. The columns of a table can be converted to column vectors if needed. A table allows you to store all of your variables, that can even be of different types, into one table in the workspace. The data can be thought of as a series of observations (rows) over a set of different variables (columns).
The command for importing a table from a file is readtable('filename')
.
The columns of a table may be accessed as column vectors using the notation
of tableName.columnName.
2.10.4. Dealing with Missing Data¶
Data values that are missing show up as NaN (Not a Number). The MATLAB
function ismissing
returns a logical vector showing where data is
missing. There are three basic ways to deal with missing data.
- Ignore it: Some MATLAB statistics functions have an option ‘omitnan’ that will ignore any NaN values.
- Delete it: Setting a table row or vector value to
[]
removes the data. - Interpolate it: MATLAB has a function called
fillmissing
that will interpolate missing data in a vector between its neighbor data. See also: Data Interpolation.
The following tutorial shows some example code for dealing with missing data.
It uses the file, mpg.txt
.
%% Dealing with missing data
% This code requires MATLAB 2016b or newer
% Run this code one section at time.
%% Read the file into a table and examine
mpg = readtable('mpg.txt');
% take a look at the part of the table
mpg(1:4,:)
% count missing data
disp('missing MPG')
nnz(ismissing(mpg.MPG))
disp('missing Weight')
nnz(ismissing(mpg.Weight))
disp('missing Horsepower')
nnz(ismissing(mpg.Horsepower))
%% Ignore missing data for mean
disp('mean MPG')
mean(mpg.MPG, 'omitnan')
%% Delete rows with missing data
% idx holds index of any rows with missing (NaN) values
idx = ismissing(mpg.MPG);
mpg(idx,:) = []; % remove rows missing values
disp('missing MPG')
nnz(idx)
%% scatter plot to observe correlation between MPG and Horsepower
scatter(mpg.Horsepower, mpg.MPG)
%% Interpolate missing data
% re-read file
mpg = readtable('mpg.txt');
% sort rows by Horsepower to group missing data with similar MPG data.
mpg = sortrows(mpg, 'Horsepower');
% find missing data
idx = find(ismissing(mpg.MPG));
disp('missing data')
for i = idx'
disp(mpg.MPG(i-2:i+2))
disp(' ')
end
% use linear interpolation
mpg.MPG = fillmissing(mpg.MPG, 'linear');
disp('interpolated data')
for i = idx'
disp(mpg.MPG(i-2:i+2))
disp(' ')
end
2.10.5. Exporting Table Data¶
Now that the data is formatted how you like it, you can use the
writetable
function to export a table to a delimited text file or
spreadsheet.
>> writetable('myTable.csv');
By default, writetable
uses the file name to determine the appropriate
output format.
Note
There is a lot more information about MATLAB tables that are not included here.
Now complete the tutorial in Working with Tables.