Student Notes · 2 Hours · MathWorks Onramp Modules 7–9
[0, 20, 40, 60, 60, 80, ...]. Looking at a list of numbers is not the most natural way to understand what happened. A plot of speed vs time shows acceleration, cruising, and braking phases at a glance. Session 3 bridges calculation and communication.Numbers in the Command Window tell you what happened. A graph shows you why it matters. Engineers communicate findings and make decisions with graphs, not with raw tables of values.
A complete engineering plot has six elements. All six are required. A plot missing any of them is an incomplete engineering document.
'b-o' (blue, solid, circles)
⑤ Dashed red = threshold reference line
⑥ ylim([0 100]) fixes the y-axis to 0–100%
| # | Element | MATLAB Command | Rule |
|---|---|---|---|
| ① | Title | title('...') | Describe what the plot shows — not just "Plot 1" |
| ② | Y-axis label | ylabel('...') | Always include the quantity and unit, e.g. ylabel('SoC (%)') |
| ③ | X-axis label | xlabel('...') | Same rule — quantity and unit, e.g. xlabel('Time (s)') |
| ④ | Line style | format string 'b-o' | Choose deliberately — especially when comparing two series |
| ⑤ | Legend | legend('...') | Required whenever there are two or more lines on the same figure |
| ⑥ | Axis limits | ylim([0 100]) | Set scale deliberately — SoC should always span 0–100% |
| Command | Purpose | Notes |
|---|---|---|
| plot(x, y) | Basic line plot | x and y must have the same number of elements |
| xlabel('...') | Label the x-axis | Always include units in brackets: 'Time (s)' |
| ylabel('...') | Label the y-axis | Same rule: 'SoC (%)', 'Speed (km/h)' |
| title('...') | Add a plot title | Should describe what the plot shows, not just the variable name |
| grid on | Add gridlines | Makes reading values off the graph much easier |
| ylim([min max]) | Set y-axis range | Use ylim([0 100]) for SoC so scale is always consistent |
| xlim([min max]) | Set x-axis range | Useful when zooming into a specific time window |
| find(v < x) | Locate threshold crossings | Returns indices where the condition is true |
find() is not strictly a plotting command, but it works alongside plots. After you see a threshold crossing in a graph, use find(soc < 50) to locate the exact index where it happens. It returns the indices (positions) in the vector where the condition is true — not the values themselves.A format string is a short text argument passed to plot() that controls the appearance of a line in one compact expression. The string can contain a colour code, a line style, and a marker — in any order.
plot(x, y, 'ColourLineMarker') — for example 'r--s' means red, dashed line, square markers. All three parts are optional — you can use one, two, or all three. No spaces inside the string.'r--s' is correct. 'r -- s' will error. The three parts (colour, line, marker) are written together as a single string.To plot two or more data series on the same figure, you need hold on. Without it, each new plot() call replaces the previous figure.
hold on must appear between the two plot() calls — not before both or after both. The sequence is always: first plot → hold on → second plot. legend() must come after all plot() calls and labels its series in the order they were plotted.Open your Onramp Command Window and complete the three steps below.
Create a time vector t with values 1 through 10 using the colon operator. Then create a SoC vector with the values [100, 97, 93, 88, 82, 75, 67, 58, 48, 37] in percent. These represent a battery discharging over 10 seconds of a drive test.
Produce a complete, labelled plot of SoC vs time. Your plot must include all six elements from the Anatomy section: a line with a format string, x-axis label with unit, y-axis label with unit, a descriptive title, gridlines, and a y-axis limit fixed to 0–100%.
Also apply 'LineWidth', 2 as a name-value pair after the format string to make the line slightly thicker.
Before moving to Step 3, ask yourself: is this plot something you could hand to another engineer and they would understand it without any further explanation?
Look at the shape of the curve and use indexing tools to answer the questions below. For Q2, use MATLAB in Onramp to find the answer rather than reading it off the graph.
find() to locate the answer programmatically — do not read it off the graph.find() returns the indices where a condition is true. What condition do you need?hold on was not used, or was placed after both plot() callsplot() → hold on → second plot(). Rerun in that order.hold on was missing entirely — each plot() opened a new figurehold on between the two plot() calls.'r -- s' instead of 'r--s''r--s'.xlabel('Time') without the unitxlabel('Time (s)'), ylabel('SoC (%)').legend() called before the second plot(), or labels listed in wrong orderlegend() must come after all plot() calls. It labels series in the order they were plotted.find(soc < 50) will return nothing if all values are ≥ 50. Adjust the threshold or confirm the vector values.plot(x, y) creates a line graph — the most-used visualisation command in engineering
xlabel, ylabel, and title are not optional — an unlabelled plot is an incomplete engineering document
hold on keeps the current figure active so you can overlay multiple data series — position it between your two plot() calls
legend() identifies each data series — required whenever two or more lines share a figure
'r--o' control colour, line style, and markers in a single argument — no spaces
find() locates the indices where a condition is true — used for threshold crossings, anomaly detection, and event logging
ylim([0 100]) fixes the y-axis scale — always set this for SoC plots so comparisons across tests are fair
You can now store data, process it, and visualise it. But every time you want to run the same analysis you have to retype all the commands. And your graph is produced once — you cannot tell MATLAB to decide what to display based on the data values.
What would it look like to write MATLAB code that makes decisions — for example, displaying a warning message when SoC drops below 20%? How would you structure that logic? — Session 4 answers this, and also completes the MATLAB Onramp certificate.