MATLAB: Introduction to the Command Window, Workspace and Assigning Variable Names

Requires updated screenshots for clear all, close all, and clc

Tutorial Video

Getting Started with MATLAB

The Command Window

The prompt >> will show in the Command Window before each line of code.

If we type something very simple like:


We will see


The Variable Workspace

ans is the default variable name for a variable created without an assigned name. We can open it in the Variable Workspace which will open the Variable Editor. This will give us the Variable in Spreadsheet format and we can click in the cell and change the value in the cell if we wanted to:

Assignment of a Variable Name

We can now type in the variable name direct in the Command Window


This will show the value of the variable


We can instead define our own Variable Name by use of the Assignment Operator


For example x is assigned using the assignment operator = to the value of 1


Here the variable name x is on the left hand side and is assigned by the assignment operator = to the numeric value 1 on the right hand side:

We can open x in the Variable Editor by clicking on it in the Workspace viewing it in a spreadsheet format:

The Assignment Operator Description

Okay in order to better understand, what's going on, let's now try inputting:


We get an error:

Error: Incorrect use of '=' operator. To assigne a value to a variable name, use '='. To compare values for equality use, '=='.

In other words we cannot reassign a core numeric character 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 to another value as this would severely mess up the program so this error very much makes sense.

Case Sensitivity of a Variable Name

Variable Names are case sensitive. We can test this by typing in:


We get

Undefined function or variable X.

Checking if a Variable Name Exists using the exist Function

We can use the function exist functions have a function name in this case exist and input arguments to the function are enclosed in circular brackets ( ) because the function name is a word consisting of multiple characters known as a string it has to be enclosed in single quotation marks ' '. This will be covered in more detail later when functions are covered:


This function is commonly used so also works in the abbreviated form:


A non-zero value is returned meaning the variable exists. In this case we can see this in the Workspace.

We can now try with the Variable Name X




This returns a 0 value meaning the variable X does not exist. Be careful when using upper and lower case when it comes to Variable Naming.

If you have a large and complicated workspace, it is sometimes useful to check if a Variable Name exists before attempting to create it. Lets try with the Variable Name y




This Variable Name doesn't exist and MATLAB returns a value of


We can now safely assign the Variable y to a Value of 2:


The Variable y is now created and appears in the Workspace:

We can double click it to open it in the Workspace:

Assigning a Variable Algebraically Using Existing Variable Names

We can now check to see if the variable z exists using:




We see that it doesn't so we can safely create it:

This time instead of assigning z directly as a numeric value, we will assign it to a value in terms of the two Variables x and y. We will assign it to the product of these two Variables using the + operator:


This gives:

% Assigned Values of x and y
% Assignment of z
% Substituting in x and y
% Value of z

We can open up both the variables x and z in the Variable Editor:

Reassigning Variables

Now we are going to update the value of x.


One may think that updating the value of x will also update the value of z because z was assigned as x+y however z remains unchanged.

z was calculated with the original value of x=1 and y=2. If x and y change later on z remains unchanged unless of course one explicitly reassigns the value of z.

Let's reassign the value of x back to 1:


We can now reassign x to a new value however this time in terms of it's original value.


This gives:

% Original Value of x
% Reassigned Value of x
% Substituting in x
% New Value of x

If you are still confused about this, we will go through this with some temporary variables. MATLAB essentially does this in the background when reassigning x in terms of x. First let's reassign x back to 1:


The Variable x has returned back to 1:

We can create another variable called xold and assign it to x


Now xold has a value of 1:

We can now create a new variable xnew and assign it in terms of xold


This gives a value


Now we can set the Variable x in terms of the Variable xnew


This gives


Clearing Variables Using the clear Function

We can use the clear function to clear the temporary variables. Like the function exist we can type it as:


Or because it is commonly used we can use the abbreviated form:


Notice how the Variable xold doesn't show in the Workspace because it no longer exists. You can verify this using the exist function if you want to

We can also clear the Variable xnew using


Or in the abbreviated form as:


Notice how xnew no longer exists in the Workspace:

Saving the Workspace using the save function

One can save the workspace using the function save function.


Once again as this function is commonly used, there is an abbreviated version:


Current Folder

The saved workspace will be in the Current Folder.

You can see its full location from the Address Bar.

You can click on the yourworkspace.mat file to preview its details.

Closing all open Figures in the Workspace

Sometimes one will also want to close a Figure or all Figures. In this case we don't have any open so we will create some blank ones to demonstrate however the function close has a similar notation to clear with the input argument being the figure number or 'all':


Closing all open Variables in the Workspace

To close all open Variables in the Workspace we use the clear function once again however instead of having the Variable Name as the functions input argument we use 'all':


Or in the abbreviated form:


Clearing the Command Window History

We use the function clear to clear Variables in the Workspace. To clear the History of the Command Window we use clc (standing for clear command). Here is all the Command Window with the previous lines shown above:

Inputting the code:


Removes all the Commands in the Command Window:

Command Windows History

On a new Prompt


One can press the [↑] to populate the current line with the previous command.

Pressing [↑] again populates the current line with the second previous command.

Pressing [↑] again populates the current line with the third previous command and so on.

If you want to remove these previous commands:

Select Clear Command History:

Select Yes:

The Command Window will be cleared:

Loading a Workspace

One can use the load function to load a workspace.


Or as it is commonly used, it has an abbreviated form:


Note by default MATLAB will attempt to open the Workspace in the Current Folder. If you want to specify a different folder you can change the input argument to the function to include the full file path. The folder including the workspace name should be specified as a string in single quotation marks ' ':


Once the workspace is loaded all the Variable Names within the workspace should display under Workspace

Additional Notes on Variable Names

There are a few rules when it comes to variable names:

  • Must start with a letter
  • Can include, letters (upper and lower case), numbers and the underscore (_) only.
  • Variable Names with symbols such as + – * / \ . , ; @ ' " will confuse MATLAB as MATLAB sees these as Operators.

You will get

Invalid Use of Operator

In most cases if you attempt to use an Operator as part of a Variable Name:

If you start with a number you will get:

Error: Invalid Expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices use brackets instead of parenthesis. 

As demonstrated the underscore can be used in the Variable Name:

Inbuilt Variables and Functions

There are a number of inbuilt Variable Names and Functions. You should avoid assigning Variable Names to these. For example the variable pi


We get a non-zero value:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.