Requires updated screenshots for clear all, close all, and clc
Table of contents
- Tutorial Video
- Getting Started with MATLAB
- The Variable Workspace
- Assignment of a Variable Name
- The Assignment Operator Description
- Case Sensitivity of a Variable Name
- Checking if a Variable Name Exists using the <code>exist</code> Function
- Assigning a Variable Algebraically Using Existing Variable Names
- Reassigning Variables
- Clearing Variables Using the <code>clear</code> Function
- Saving the Workspace using the <code>save</code> function
- Current Folder
- Closing all open Figures in the Workspace
- Closing all open Variables in the Workspace
- Clearing the Command Window History
- Command Windows History
- Loading a Workspace
- Additional Notes on Variable Names
- Inbuilt Variables and Functions
Tutorial Video
Getting Started with MATLAB
The Command Window
The prompt
If we type something very simple like:

We will see
ans=1

The Variable Workspace

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
ans=1

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

Here the variable name

We can open

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

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
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
Or

This returns a

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
Or

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

We can now safely assign the Variable

The Variable

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
Or:

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

This time instead of assigning

This gives:
% Assigned Values of x and y
x=1
y=2
% Assignment of z
z=x+y
% Substituting in x and y
z=1+2
% Value of z
z=3

We can open up both the variables

Reassigning Variables
Now we are going to update the value of

One may think that updating the value of


Let's reassign the value of

We can now reassign

This gives:
% Original Value of x
x=1
% Reassigned Value of x
x=x+1
% Substituting in x
x=1+1
% New Value of x
x=2

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

The Variable

We can create another variable called

Now

We can now create a new variable

This gives a value
xnew=2

Now we can set the Variable

This gives
x=2

Clearing Variables Using the
clear
Function
We can use the
Or because it is commonly used we can use the abbreviated form:

Notice how the Variable

We can also
Or in the abbreviated form as:

Notice how

Saving the Workspace using the
save
function
One can save the workspace using the 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



Closing all open Variables in the Workspace
To close all open Variables in the Workspace we use the
Or in the abbreviated form:

Clearing the Command Window History
We use the function

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
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:
