Matlab Onramp course notes - Part 3 Plots & Data Import
Studying the self paced course in matlab academy for academic work - Part 3
Matlab Onramp course notes - Part 3 Plots & Data Import
Plots
Plot Vectors
- Plot two same-length vectors:
1
plot(x, y)
- Task — plot sample vs mass1:
1
plot(sample, mass1)
- Specify color/line/marker with a line spec string:
1
plot(x, y, "r--o") % "r--o" = red, dashed, circle markers
- Task — plot sample vs mass2 with red stars and no line:
1
plot(sample, mass2, "r*")
- To overlay plots, use hold on:
1 2 3
plot(x1, y1) hold on plot(x2, y2)
- Task — hold on; then plot sample vs mass1 with magenta squares and no line:
1 2
hold on plot(sample, mass1, "ms")
- Stop overlaying:
1
hold off
- Plotting a single vector uses indices 1:n for x:
1
plot(y)
- Task — plot v1:
1
plot(v1)
- Use name-value arguments to set properties (e.g., line width):
1
plot(y, LineWidth=5)
- Task — plot v1 with LineWidth 3:
1
plot(v1, LineWidth=3)
- Name-value can follow line spec:
1
plot(x, y, "ro-", LineWidth=5)
- Task — plot sample vs v1 with red circles, solid line, LineWidth 4:
1
plot(sample, v1, "ro-", LineWidth=4)
- Try other plotting functions and set properties by name:
1
histogram(density, FaceColor="y")
- Note: older MATLAB (<R2021a) uses commas and quoted names:
1
plot(v1, "LineWidth", 3)
Further practice:
1
2
3
4
5
plot(v1)
hold on
plot(v2)
hold off
plot(v1, "LineWidth", 3)
Annotate Plots
- Add title:
1
title("Plot Title")
- Task — add title “Sample Mass” (example full snippet):
1 2 3 4 5 6 7 8 9 10 11 12
load datafile sample = data(:,1); density = data(:,2); v1 = data(:,3); v2 = data(:,4); mass1 = density .* v1; mass2 = density .* v2; plot(sample, mass1, "ms") hold on plot(sample, mass2, "r*") hold off title("Sample Mass")
- Add axis labels:
1
ylabel("Y-Axis Label")
- Task — add y-axis label “Mass (g)”:
1
ylabel("Mass (g)")
- Add a legend:
1
legend("a", "b", "c")
- Task — legend with “Exp A” and “Exp B” in that order:
1
legend("Exp A", "Exp B")
- Use variables in annotations (string concatenation):
1 2
bar(data(3,:)) title("Sample " + sample(3) + " Data")
Data Import
- Files panel: browse files. Select + Import Data or double-click to import.
- Double-click MAT -> variables load to workspace.
- Double-click image -> imported as numeric pixel array.
- Double-click spreadsheet -> Import Tool opens (many possible formats).
- Preview shows import selection in blue.
- Missing data highlighted orange; default replacement = NaN.
- Default import = table (columns can have mixed types; rows align).
- First row detected as headers → used as table variable names.
- Import Tool infers each variable’s data type.
- Output variable name comes from file name.
- Click Import Selection to create the workspace variable.
Import Data as a Table
- Extract table variable via dot notation:
1
var = table.VariableName
Task — Assign elements.Density to a column vector d:
1
2
3
load datafile
elements
d = elements.Density
- To append calculations to a table, assign a new variable:
1
data.HeightMeters = data.HeightYards * 0.9144
- If data.HeightMeters doesn’t exist, MATLAB creates it.
Task — Multiply elements.Density with elements.Volume1 (element-wise) and save as elements.Mass:
1
2
3
4
mass = elements.Density .* elements.Volume1
mass
elements.Mass = mass
elements = sortrows(elements, "Mass")
- You can click a table in a live script output to interact (sort, etc.).
- To save edits back to code, click Update Code in the output pane.
To sort by Mass via the UI: scroll right to Mass column → Sort Smallest to Largest → Update Code.
- Use dot notation to extract table variables; use array indexing to extract rows.
1
top3 = elements(1:3,:)
- top3 is a table.
1
elements(1:3,:)
This post is licensed under CC BY 4.0 by the author.