MATLAB Onramp Progress after Session 1 Modules 1–3 of 12  (25%)
📋

Session Overview

This session introduces you to the MATLAB programming environment. You will work entirely inside MathWorks Onramp — a browser-based MATLAB environment that requires no local installation.

By the end you will be able to type commands, create and name variables, perform arithmetic, and explain what each part of the MATLAB interface does.

🧠
Block A — 15 min
Theory: MATLAB environment, variables, data types
⌨️
Blocks B & C — 90 min
Onramp Modules 1–3: commands, variables, numeric data
Block D — 15 min
EV task: battery variables and current calculation
Objective
After this session you should be able to navigate Onramp, create and name variables, assign values, perform arithmetic, and explain what the Workspace panel shows — all in the context of EV engineering data.
🖥️

The MATLAB Environment

MATLAB has four key areas. In Onramp, all of these are available in your browser window. Understanding where each one is will save you a lot of confusion during the exercises.

Diagram — MATLAB environment flow
flowchart LR A["⌨️ Command Window\n(you type here)"]:::cyan -->|"runs"| B["📦 Workspace Panel\n(variables stored here)"]:::green A -->|"produces"| C["📊 Figure Window\n(plots appear here)"]:::orange D["📄 Script Editor\n(.m files)"]:::purple -->|"executes all lines"| A classDef cyan fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold classDef green fill:#D1FAE5,stroke:#059669,color:#065F46,font-weight:bold classDef orange fill:#FEF3C7,stroke:#D97706,color:#92400E,font-weight:bold classDef purple fill:#EDE9FE,stroke:#7C3AED,color:#5B21B6,font-weight:bold

Command Window

The >> prompt is where you type commands. Press Enter to run. Results appear immediately below your command. This is where you do all your work in Sessions 1–4.

Workspace Panel

Every variable you create appears here with its name, value, and type. Think of it as a whiteboard — it shows everything MATLAB currently remembers. Cleared when you close Onramp.

Figure Window

Plots and charts open here when you call plot(). You will use this from Session 3 onwards. For today, ignore it.

Script Editor (.m files)

For saving sequences of commands. Introduced properly in Session 4. Scripts let you run and re-run code without retyping everything.

Your first commands — try these in Onramp

% Type each line in the Command Window and press Enter >> 3 + 5 ans = 8 >> sqrt(144) ans = 12 >> 400 * 1.25 ans = 500
ans
When you type an expression without assigning it to a variable, MATLAB stores the result in a special automatic variable called ans. It gets overwritten every time you run a new expression — so always name your variables properly.
📦

Variables

A variable is a named memory container. You create one by typing name = value and pressing Enter. MATLAB immediately stores the value and shows it in the Workspace panel.

Diagram — how a variable assignment works
flowchart LR A["You type\nbattery_voltage = 400"]:::step1 B["MATLAB parses\nname = value"]:::step2 C["Workspace stores\nbattery_voltage → 400"]:::step3 D["You can use it\nin any calculation"]:::step4 A --> B --> C --> D classDef step1 fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold classDef step2 fill:#F0FDF4,stroke:#059669,color:#065F46,font-weight:bold classDef step3 fill:#EDE9FE,stroke:#7C3AED,color:#5B21B6,font-weight:bold classDef step4 fill:#FEF3C7,stroke:#D97706,color:#92400E,font-weight:bold
% Creating variables — type each line and press Enter battery_voltage = 400 motor_power = 150 soc_percent = 87

After each line, look at the Workspace panel. You should see each new variable appear with its value. If you type the variable name alone and press Enter, MATLAB prints its current value.

Case‑Sensitive
MATLAB is case-sensitive. Voltage and voltage are two completely different variables. Be consistent — pick a style and stick to it throughout a session.
Semicolons
Adding a semicolon after a line (voltage = 400;) tells MATLAB to run the command but not print the result. Useful when you have many variables and don't want the screen flooded with output. You will use this more from Session 4 onwards.
🔢

Data Types at a Glance

MATLAB figures out the data type automatically when you assign a value — you never need to declare it explicitly. For this session, everything will be a number.

Diagram — MATLAB data types
graph TD ROOT["MATLAB Data Types"]:::root ROOT --> A["Scalar\nbattery_voltage = 400\nsingle number"]:::cyan ROOT --> B["String\nunit = 'volts'\ntext in quotes"]:::green ROOT --> C["Logical\ncharging = true\ntrue (1) or false (0)"]:::orange ROOT --> D["Array / Vector\nspeed = [0,20,40,60]\nmultiple values — Session 2"]:::purple classDef root fill:#F1F5F9,stroke:#CBD5E1,color:#1A2B3C,font-weight:bold,font-size:15px classDef cyan fill:#E0F2FE,stroke:#0284C7,color:#0369A1 classDef green fill:#D1FAE5,stroke:#059669,color:#065F46 classDef orange fill:#FEF3C7,stroke:#D97706,color:#92400E classDef purple fill:#EDE9FE,stroke:#7C3AED,color:#5B21B6
Today's Focus
For Sessions 1 and 2, all your variables will be numeric scalars (single numbers). MATLAB defaults to double-precision floating point — extremely accurate for engineering calculations. Strings and logicals come in Session 4 when we write decision logic.

Useful commands for inspecting variables

% Check a variable's type class(battery_voltage) % returns 'double' % Check a variable's size (more useful for arrays) size(battery_voltage) % returns [1, 1] for a scalar % Display a value with a label disp(battery_voltage) % prints: 400
📝

Variable Naming Rules

Choosing good variable names is a professional habit, not just a MATLAB requirement. Clear names make code readable months later.

Diagram — valid vs invalid naming
flowchart TD Q{"Does the name..."}:::q Q -->|"Start with a letter?"| R1{"Yes"} Q -->|"Start with a number?"| FAIL1["❌ INVALID\n400voltage"]:::bad R1 -->|"Contain only letters,\nnumbers, underscores?"| VALID["✅ VALID\nbattery_voltage"]:::good R1 -->|"Contain a space\nor special character?"| FAIL2["❌ INVALID\nbattery voltage"]:::bad VALID -->|"Matches a reserved keyword\n(if, for, end, while...)?"| FAIL3["❌ INVALID\nend = 400"]:::bad VALID -->|"No conflict"| OK["✅ Safe to use"]:::good classDef q fill:#F1F5F9,stroke:#94A3B8,color:#1A2B3C,font-weight:bold classDef good fill:#D1FAE5,stroke:#059669,color:#065F46,font-weight:bold classDef bad fill:#FEE2E2,stroke:#DC2626,color:#991B1B,font-weight:bold
battery_voltage = 400 ✅ VALID lowercase, underscore separator — preferred style
PackCapacity = 75 ✅ VALID CamelCase also works
motor power = 150 ❌ INVALID spaces are not allowed in names
400voltage = 400 ❌ INVALID must start with a letter
Voltage ≠ voltage ⚠️ DIFFERENT case-sensitive — these are two separate variables
end = 400 ❌ RESERVED end, for, if, while, function are MATLAB keywords
Convention
Professional MATLAB code uses snake_case — lowercase words separated by underscores. Examples: motor_torque, pack_voltage, cell_temperature. Avoid single-letter names like v or x — they tell nobody what the value represents.

EV Context — Why This Matters

Every sensor on an electric vehicle produces a value. Every value needs to be stored, labelled, and used in a calculation. MATLAB variables are the mechanism that makes this possible.

Real EV parameters as MATLAB variables

% Key parameters for a generic battery-electric vehicle battery_voltage = 400; % V — 400 V pack (Hyundai Ioniq 5, MG ZS EV) pack_capacity = 75; % kWh — usable energy storage motor_power = 150; % kW — peak motor output soc_percent = 87; % % — current state of charge motor_rpm = 4200; % rpm — motor shaft speed ambient_temp = 32; % °C — outside temperature

The comments after each line (starting with %) document the unit and context. This is how professional engineers write MATLAB code — the unit is always noted.

Diagram — Block D calculation chain
flowchart LR A["battery_voltage\n400 V"]:::input B["motor_power\n150 kW"]:::input C["Unit conversion\nkW × 1000 = W\n150 000 W"]:::convert D["Formula\nI = P ÷ V"]:::formula E["max_current\n375 A"]:::result B --> C --> D A --> D --> E classDef input fill:#E0F2FE,stroke:#0284C7,color:#0369A1,font-weight:bold classDef convert fill:#FEF3C7,stroke:#D97706,color:#92400E,font-weight:bold classDef formula fill:#EDE9FE,stroke:#7C3AED,color:#5B21B6,font-weight:bold classDef result fill:#D1FAE5,stroke:#059669,color:#065F46,font-weight:bold,font-size:15px
Units Matter
MATLAB does not know what units your variables are in — that is the engineer's responsibility. The formula P = V × I requires power in Watts. If you store power in kilowatts and forget to convert, you get a result that is 1000× too small. Always track your units with comments.
Real World
The Porsche Taycan uses an 800 V architecture. At the same 150 kW output, max_current = (150 × 1000) / 800 = 187.5 A — roughly half the current of a 400 V system. This is exactly why high-voltage platforms use thinner, lighter cables.
🔧

Block D — EV Task

Block D ⏱ 15 min Battery Variables & Current Calculation

Open your Onramp Command Window and complete the following four steps.

1
Define three EV variables

Create variables for battery_voltage (400 V), pack_capacity (75 kWh), and motor_power (150 kW). After each line, confirm the variable appears in your Workspace panel before continuing.

2
Calculate and store maximum current

Using the variables you just created and the relationship P = V × I, write a single MATLAB expression that calculates max_current. Think carefully about units before you write the expression.

3
Verify your result

Display max_current in the Command Window. Check whether your result is physically reasonable for a high-power EV system. If something looks unexpected, review your expression and the unit of motor_power.

4
Reflect on these questions

Think through the questions below before the group discussion. You do not need to write code for these — they are conceptual.

Q1 — Why did the expression need to account for the unit of motor_power?
Hint: think about the units on each side of P = V × I
Q2 — Which real EV component must be rated to handle at least this current value?
Hint: think about what sits between the battery and the motor
Q3 — If battery_voltage dropped to 350 V while motor_power stayed at 150 kW, what would happen to max_current? Recalculate in Onramp to confirm your reasoning.
Hint: update battery_voltage in Onramp and re-run your expression
Note
This task is intentionally left for you to work through. The steps above give you the structure — the thinking and the expressions are yours to write. If you get stuck, re-read the Variables and EV Context sections above before asking for help.

Troubleshooting — if something goes wrong

❌ Undefined variable error
Cause: Typo in variable name, wrong case, or the variable was never created
Fix: Check the Workspace panel. If a variable is missing, retype it exactly. Remember: MATLAB is case-sensitive.
⚠️ Result looks too small (0.375 instead of 375)
Cause: Motor power was used in kW without converting to Watts
Fix: Revisit the unit of motor_power and rethink the expression.
❌ Invalid variable name error
Cause: Space in name (e.g. motor power) or name starts with a number
Fix: Use underscores instead of spaces: motor_power. Names must start with a letter.
🔍 Workspace panel not visible
Cause: Panel was accidentally closed
Fix: Use the View menu in Onramp to restore it, or reload the Onramp page.

Key Takeaways

⌨️ MATLAB is command-driven — type a command, press Enter, see the result immediately
📦 Variables are named memory containers — created with name = value
👁️ The Workspace panel tracks every variable currently stored — your real-time data inventory
📝 Names must start with a letter, contain no spaces, and are case-sensitive
🔢 MATLAB infers data types — for now everything is a numeric double-precision scalar
Every EV sensor reading — voltage, current, temperature, SoC — is a variable in an engineer's MATLAB script
% Comments starting with % document your code — always note the unit of every variable

Before Session 2 — think about this

If an EV logs its speed every second over a 10-second test, you would need 10 separate variables: speed1, speed2speed10. A full WLTP drive cycle has 1800 data points. There has to be a better way to store all of that.

What do you think that better structure might look like? — Session 2 answers this.

Session 2 →