MATLAB Workspace Management

When you calculate in MATLAB, a command can depend on values left by earlier commands. The workspace is the collection of variables currently stored in memory and available to your commands. MATLAB Workspace Management means knowing which names exist, what they contain, and which results need recalculating. Here, you enter commands directly in the Command Window.

Create named values

An assignment evaluates an expression and stores its result under a variable name. In MATLAB, the equals sign performs this operation: the name goes on the left and the expression goes on the right. You do not need to declare an ordinary numeric variable before assigning its first value.


Example: You record a distance of 8 m and a travel time of 4 s. These commands create three variables:


MATLAB
distance_m = 8;
time_s = 4;
speed_mps = distance_m/time_s;


The calculation is


v=dt=8 m4 s=2 m/s.v = \frac{d}{t} = \frac{8\ \text{m}}{4\ \text{s}} = 2\ \text{m/s}.


The ending semicolons suppress printed results; they do not prevent storage. MATLAB stores ordinary numbers without attaching physical units, so names such as distance_m help you keep the units consistent.


Choose descriptive names beginning with a letter and containing letters, digits, or underscores. MATLAB distinguishes uppercase and lowercase letters: time_s and Time_s are different names. Avoid names already used by functions, such as sum, because a variable can interfere with calling the function.

Read assignments in time order

An assignment uses the values available when that command executes. Its governing rule is


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


Three connected boxes show how MATLAB reads current values, evaluates an expression, and replaces the value on the left.


Continuing the example, suppose you correct the distance:


MATLAB
distance_m = 10;


Now distance_m contains 10, but speed_mps still contains 2. The earlier division stored a number; it did not store an instruction to repeat the division whenever an input changes. To obtain the corrected speed, execute speed_mps = distance_m/time_s; again. It then stores 2.5 m/s.


Likewise, saved_speed = speed_mps; stores the current numeric value under another name. Later reassignment of speed_mps does not change saved_speed for these ordinary numeric variables.



Inspect and remove variables

The Workspace panel provides a visual inventory. At the command prompt, who lists variable names, while whos also reports sizes, memory usage, and classes. A class describes the stored data type; ordinary numeric literals normally create values of class double. Typing an existing variable's name displays its value.


Command

Immediate effect

who

Lists current variable names

whos

Lists names with sizes and data types

clear distance_m

Removes the variable named distance_m

clear

Removes all variables from the current workspace

clc

Removes displayed text from the Command Window


Clearing displayed text does not clear stored variables. Conversely, removing an input variable does not remove numeric results already calculated from it. Those results remain available, although repeating the original calculation requires the missing input to be defined again.



What to remember

  • Assignment stores a result calculated from current values.
  • Changing an input does not automatically update earlier results.
  • Use who or whos to inspect the workspace.
  • Use clear to remove variables and clc to clear displayed text.

Example: MATLAB Workspace Management for a cart test

You time a cart over two consecutive track sections. It travels 12 m in 2 s, then 9 m in 5 s. The workspace state is the set of variable names and values currently stored. Track that state as you calculate a speed, correct a timing measurement, and remove an input. MATLAB stores the numbers; the variable names indicate the intended units.


Start with these commands:


MATLAB
clear
distance_m = 12 + 9;
elapsed_s = 2 + 5;
speed_mps = distance_m/elapsed_s;
saved_mps = speed_mps;


a) What values are stored in speed_mps and saved_mps after these commands?


First, combine the two distances and the two travel times. The total distance is ________, and the total elapsed time is ________. These totals are stored before the division executes:


d=12+9=21 m,t=2+5=7 s.\begin{aligned} d &= 12 + 9 = 21\ \text{m},\\ t &= 2 + 5 = 7\ \text{s}. \end{aligned}


Next, calculate the speed using those current values:


v=dt=21 m7 s=3 m/s.v = \frac{d}{t} = \frac{21\ \text{m}}{7\ \text{s}} = 3\ \text{m/s}.


The last assignment copies this numeric result into saved_mps. It does not move the result out of speed_mps.


Answer: speed_mps and saved_mps both store 3 m/s.


b) You discover that the recorded elapsed time omitted a 3 s pause. Enter elapsed_s = elapsed_s + 3;. What speed remains stored before recalculation? Then execute speed_mps = distance_m/elapsed_s; and report the recalculated speed and saved copy.


Evaluate the time update using the old value on the right side:


tnew=7+3=10 s.t_{\text{new}} = 7 + 3 = 10\ \text{s}.


The updated elapsed time is ________. This assignment changes only elapsed_s, so speed_mps still stores 3 m/s immediately afterward. Updating an input does not repeat an earlier calculation.


When you explicitly execute the division again, MATLAB uses the current distance and updated time:


vnew=21 m10 s=2.1 m/s.v_{\text{new}} = \frac{21\ \text{m}}{10\ \text{s}} = 2.1\ \text{m/s}.


This replaces the value in speed_mps. It does not change saved_mps, which received its value earlier. The smaller recalculated speed is consistent with covering the same distance in a longer total time.


Answer: Before recalculation, speed_mps stores 3 m/s. After recalculation, it stores 2.1 m/s, while saved_mps still stores 3 m/s.


c) Continue from part b by executing clear distance_m and then clc. Which variables remain, and can you immediately repeat speed_mps = distance_m/elapsed_s; successfully?


The command clear distance_m removes only the named distance variable. The other three variables retain their current numeric values. The following clc command removes displayed text without changing those variables.


You can inspect the remaining names and properties using whos. The remaining variables are elapsed_s, storing 10 s; speed_mps, storing 2.1 m/s; and saved_mps, storing 3 m/s.


Repeating the original division would require distance_m on its right side. That variable is now absent, so the command produces an error rather than a newly calculated speed. You would need to define the distance again before repeating that division successfully.


Answer: The three remaining values are 10 s, 2.1 m/s, and 3 m/s, under their respective names. The division cannot yet be repeated successfully because distance_m has been removed.

A laboratory assistant wants to inspect the names, sizes, and data types of MATLAB variables before continuing a calculation. Which command provides this information without removing the variables?

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

You are entering a sensor gain and an explanatory note in the MATLAB Command Window. Fill each blank with one punctuation character.


To execute gain = 1.25 without displaying its result, append immediately after 1.25.


On the next line, place before the text Gain measured during calibration so MATLAB treats that entire line as explanatory text rather than an instruction.

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

A sensor correction subtracts an offset from a raw reading. A technician enters these MATLAB commands in order, with all readings measured in millivolts:


MATLAB
clear
raw_mV = 14;
offset_mV = 2;
corrected_mV = raw_mV - offset_mV;
offset_mV = 3.5;
difference_mV = corrected_mV - (raw_mV - offset_mV);


What value is stored in difference_mV? Answer in mV, to one decimal place.

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