MATLAB Workspace

The MATLAB Workspace holds the variables available for your calculations. During an engineering calculation, it lets you check which inputs and results currently exist, what values they contain, and whether an earlier result needs updating. Understanding the workspace helps you explain unexpected results without confusing stored data with text displayed on the screen.

Stored values and visible output

A workspace is a collection of variables currently available in an execution context. Here, you will work in the base workspace, where variables entered directly in the Command Window are stored. The Command Window accepts commands and displays output; the Workspace panel lets you inspect stored variables. Their positions can vary with the desktop layout, but their roles remain distinct.


For example, this command creates a variable representing a force of 60 N60\ \text{N}:


MATLAB
force_N = 60;


The semicolon suppresses the displayed result. MATLAB still executes the assignment and stores the value. Entering force_N without a semicolon displays its current value without changing it.


An evaluated command updates a workspace variable and can also produce displayed output.


An ordinary numeric variable stores a number, not its physical unit. A name such as force_N documents your intended unit; MATLAB does not enforce that interpretation for an ordinary numeric value. Keep units consistent yourself.

Assignment records a value now

An assignment evaluates the expression on the right of = and stores its result under the name on the left. For the numeric variables considered here, the governing rule is:


new stored value←expression evaluated using current values\text{new stored value} \leftarrow \text{expression evaluated using current values}


Consider these commands, with mass in kilograms and acceleration in metres per second squared:


MATLAB
force_N = 60;
mass_kg = 12;
accel_mps2 = force_N/mass_kg;


The stored acceleration is:


a=60 N12 kg=5 m/s2a = \frac{60\ \text{N}}{12\ \text{kg}} = 5\ \text{m/s}^2


Now executing mass_kg = 15; changes the mass variable. It leaves accel_mps2 at 5 m/s25\ \text{m/s}^2, because that variable contains the result of an earlier calculation. To obtain the result for the new mass, execute the acceleration assignment again:


aupdated=60 N15 kg=4 m/s2a_{\text{updated}} = \frac{60\ \text{N}}{15\ \text{kg}} = 4\ \text{m/s}^2



Assignment can also update a variable using its own previous value. For example, mass_kg = mass_kg + 2; reads the existing value before replacing it. If the mass was 15 kg15\ \text{kg}, it becomes 17 kg17\ \text{kg}.

Inspecting and clearing the workspace

Use inspection commands to establish what exists before changing or deleting anything. who lists variable names. whos also reports information such as size, class and memory usage. For an ordinary numeric scalar, a size of 1×11\times1 means one row and one column; the class commonly appears as double.


Command

Effect on stored variables

Effect in the Command Window

who

Leaves variables unchanged

Lists variable names

whos

Leaves variables unchanged

Lists names and variable details

clear force_N

Removes only force_N

Does not erase previous output

clear

Removes all variables from the current workspace

Does not erase previous output

clc

Leaves variables unchanged

Clears displayed text


Clearing an input does not remove results previously calculated from it. Those results remain stored, although recalculating them requires the input to exist again. Similarly, old output can remain visible after its variable has been cleared: displayed text is not proof of the current workspace state.


Variable names are case sensitive: force_N and Force_N are distinct names. Use consistent spelling when inspecting, calculating or clearing variables. The Current Folder lists files on disk; it is separate from the variables currently held in memory.



What to remember

  • The workspace stores variables; the Command Window displays text.
  • A semicolon suppresses display while allowing assignment.
  • Updating an input leaves earlier numeric results unchanged until you recompute them.
  • whos inspects variables, clear removes variables, and clc clears displayed text.

Example: Tracking a power calculation in the MATLAB Workspace

An engineer checks the electrical input power of a DC device using P=VIP=VI, where voltage is in volts, current is in amperes and power is in watts. Starting with an empty base workspace, the engineer enters:


MATLAB
voltage_V = 12;
current_A = 0.8;
power_W = voltage_V*current_A;


a) Find the stored power. Explain why the assignments produce no displayed results.


The first two commands store the voltage and current. The third uses their current values to calculate power:


P=VI=(12 V)(0.8 A)=9.6 WP = VI = (12\ \text{V})(0.8\ \text{A}) = 9.6\ \text{W}


The workspace therefore contains voltage_V = 12, current_A = 0.8 and power_W = 9.6. Each assignment ends with a semicolon, which suppresses its displayed result while allowing the calculation and storage to occur. The stored power is 9.6 W9.6\ \text{W}.


b) The engineer next enters current_A = 1.1;. What power remains stored immediately afterward? Give the command needed to update it and calculate the updated power.


The new command replaces only the value in current_A. The earlier numeric result in power_W remains 9.6 W9.6\ \text{W} because its assignment has not executed again.


To update the power, enter:


MATLAB
power_W = voltage_V*current_A;


MATLAB now evaluates the expression with the unchanged voltage and updated current:


Pupdated=(12 V)(1.1 A)=13.2 WP_{\text{updated}} = (12\ \text{V})(1.1\ \text{A}) = 13.2\ \text{W}


The stored power becomes 13.2 W13.2\ \text{W}. Updating an input and recomputing an output are separate actions.


c) After completing the recomputation, the engineer enters the following commands. Identify the remaining variables and explain whether the original power assignment can immediately be executed again.


MATLAB
clear current_A
clc
whos


The selective clearing command removes current_A only. It leaves voltage_V at 12 V12\ \text{V} and power_W at 13.2 W13.2\ \text{W}. The following clc clears displayed text without changing either stored value. Finally, whos displays information about the two remaining variables; both are ordinary numeric scalars of size 1×11\times1 and class double.


The original assignment requires both named inputs:


MATLAB
power_W = voltage_V*current_A;


It cannot successfully recompute the power at this point because current_A has been removed. The engineer must first assign the intended current again. The previously stored power remains 13.2 W13.2\ \text{W}; removing its input does not remove that result.

An engineering student has several variables in the MATLAB base workspace. The student wants to list each variable's name, size and class without changing any stored values. Which command provides this information?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

During a laboratory session, you need to enter a calculation, inspect a variable already in memory, recall an earlier statement and browse a project folder. Match each MATLAB desktop tool to its primary purpose.

Match each term
A.

Enter individual statements at the prompt and view their displayed results.

B.

Review previously entered statements and select them for reuse.

C.

Inspect the variables currently held in memory and their properties.

D.

Browse files and folders at the current location on disk.

Workspace panel

Command Window

Files panel

Command History

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Starting with an empty MATLAB base workspace, a technician enters these commands in order. Both variables represent voltage readings in volts.


MATLAB
reference_V = 2.4;
reading_V = 7.2;
clear reference_V
clc


Which statement describes the workspace after the final command?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice