MATLAB For Data Processing and Visualization Quick Reference
MATLAB For Data Processing and Visualization Quick Reference
1. Getting Started
Summary: Getting Started with the Data
hurrs = readtable("hurricaneData1990s.txt",...
The readtable function creates a table in MATLAB from a data
"NumHeaderLines",5,"CommentStyle","##");
file.
scatter(hurrs.Windspeed,hurrs.Pressure)
hurrs.Country = categorical(hurrs.Country);
The categorical function creates a categorical array from data.
t = hurrs.Timestamp;
By default, the readtable function may import certain variables in
the table as datetime .
h = hour(t);
histogram(h)
The hour function returns the hour numbers of the input datetime
values.
2. Preprocessing Data
Review - Preprocessing Data
Missing Data
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 1/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
data = readtable("myfile")
data =
4×2 table
Var1 Var2
When you import data into MATLAB, missing numerical values are
____ ____
replaced with NaN, which stands for Not a Number.
7 0.81
1 NaN
9 0.13
10 0.91
v = mean(data.Var2)
When you calculate statistics on arrays that contain NaNs, the v =
result in another NaN.
NaN
v = mean(data.Var2,"omitnan")
v =
To ignore NaNs in the calculation, use the "omitnan" flag.
0.6167
cleaned = rmmissing(data)
cleaned =
3×2 table
Var1 Var2
You can delete rows containing missing data with rmmissing .
____ ____
7 0.81
9 0.13
10 0.91
c = categories(x)
c =
4×1 cell array
Use the categories function
to get a list of unique
{'large' }
categories.
{'medium'}
{'red' }
{'small' }
x =
Merge different categories with
1×6 categorical array
the mergecats function.
x = renamecats(x,"red","color")
x =
Rename categories with the
1×6 categorical array
renamecats function.
Ranges in continuous data can represent categories. Categorize continues data into discrete bins with the discretize
function.
>> y = discretize(X,edges,"Categorical",cats)
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 2/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
Outputs Inputs
y If the "Categorical" option is set, y is a X Array of continuous
categorical array. Otherwise, y is numeric data. X is usually
bin values. numeric or datetime.
plot(x,y)
plot(x,y,"o-","MarkerSize",8,"MarkerFaceColor","r")
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 3/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
grid("on")
grid("minor")
axis("square")
You can also customize the appearance xlim([0 8])
of existing plots. Here are a few common
graphics customization functions:
Function Controls
4. Review Project 1
A datastore is a reference to a file or set of files. The datastore function informs where to find the files.
Code Description
If your data isn't formatted the way datastore expects, you can set the datastore properties. Examples of common properties
are shown below. You can find all the properties in the the documentation.
>> ds = datastore(filename,"Delimiter","-","TextscanFormats","%D%C%f","SelectedVariableNames",var)
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 4/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
Outputs Inputs
ds Reference to a collection of data. filename File location.
"Delimiter","-" Delimiter is
one or more
characters
that separate
data values
in the file.
"TextscanFormats","%D%C%f" Import
variables
using the
output class
in the format
specification
string.
Merging Data
Once you read in multiple tables, you may want to join them together. You can join two tables in many ways. The various join
functions are listed in the table below.
Function Example
join
Key1 in Tright
must have unique
values and contain
every key in
Tleft .
innerjoin
outerjoin
outerjoin with
"MergeKeys" on
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 5/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
petdata = readtable("petdata.txt","Format","%C%C%f")
The table petdata has two categorical
5×3 table
variables, Species and Color .
[grpS,speciesVals] = findgroups(petdata.Species)
grpS =
1
The findgroups function will return a group
2
number for each element in an array.
1
1
The second output is the name associated
2
with each group number. Here, the value 1
speciesVals =
means cat .
2×1 categorical array
cat
fish
splitapply(@mean,Weight,grpS)
The splitapply function will peform a
calculation on each inputted group. ans =
11.3333
You can interpret this code as "What is the 0.6100
average weight of each species?"
[grpC,colorVals] = findgroups(petdata.Color)
splitapply(@min,Weight,grpC)
grpC =
2
2
findgroups and splitapply are commonly
1
used together. This code answers "What is the
3
minimum weight of each color?"
1
colorVals =
Notice that grpC has values 1, 2, and 3
3×1 categorical array
because there are three different colors in the
black
data. colorVals contains the meaning for
orange
each value.
white
ans =
0.5400
0.6800
8.0000
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 6/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
bar(maxWeight)
xticklabels(speciesVals)
ylabel("Weight")
legend(colorVals)
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 7/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
All graphics objects are part of a hierarchy. Most graphics objects consist of a figure window,
containing one or more axes, which contain any number of plot objects.
You can use the graphics object hierarchy to modify specific graphics objects after a plot is created.
If you stored a handle to Figure , you could use the Children properties to modify the Bar plot.
8. Review Project 2
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 8/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
data = readtable("my3Ddata")
plot3(data.x,data.y,data.z,'.')
x y z
_________ ________ ___________
xvec = -2:.2:2;
To interpolate the data onto a grid, start
yvec = -2:.05:2;
by defining the grid points. Here, yvec is
denser than xvec .
[xgrid,ygrid] = meshgrid(xvec,yvec);
The meshgrid function will convert your
vectors into the grid expected by surf
and pcolor .
zgrid = griddata(data.x,data.y,data.z,xgrid,ygrid);
Then use the griddata function to
interpolate your data onto the grid.
surf(xgrid,ygrid,zgrid);
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 9/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
im = pcolor(xgrid,ygrid,zgrid);
im.EdgeAlpha = 0;
imagesc(xvec,yvec,zgrid);
To import data from files where the formatting changes and must be inferred from the data itself, you can use functions that
allow you to interact directly with files.
fid = fopen("myfile");
Open the file and store the
file identifier. You'll use fid
with the other low-level
import functions.
firstLine = fgetl(fid)
You can import files line-by-
line using fgetl . firstLine =
frewind(fid)
To return back to the
beginning of the file, you can
rewind the file position
indicator.
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 10/11
5/23/2021 MATLAB for Data Processing and Visualization - Quick Reference
fclose(fid);
When you're finished
importing, make sure you
close the file connection.
11. Conclusion
https://matlabacademy.mathworks.com/artifacts/quick-reference.html?course=mlvi&release=R2021a&language=en 11/11