MATLAB - Workspace



In matlab workspace, also called workspace browser, is a place where you will find all the variables along with its most recent values stored in memory. All the variables available in workspace are the ones which you have used while writing your code or have imported data from other programs or files.The workspace acts as a temporary storage and allows you to create, read,update or delete the variables.

The workspace reference for the stored variables are available only if this matlab is open, once close the workspace is erased.But incase if you want to use it in future you can always save it with .mat extension.

Let us understand the workspace in more detail in this section.

  • Opening of workspace browser in matlab
  • Creating and editing variables in workspace
  • Save and Load Workspace Variables
  • Clearing Workspace

Opening of Workspace Browser in Matlab

Open matlab, most of the time workspace is by default selected as part of the layout, in case you don't see it. Here are a few ways to open a workspace for you.

Go to Home tab, inside the Environment section click on layout as shown below −

environment section

Open layout and it should show the following details

layout options

Incase workspace is not selected for you , select it and you should see workspace as part of the layout as shown below.

workspace

You can make use of the command: workspace in the matlab command window and once it executes it will open the workspace.

>>workspace

Creating and Editing Variables in Workspace

Let us create a few variables in the command window and see the same being stored in the workspace.

Below we have created a matrix A in command window

>> A = [1 2 3; 4 5 6; 7 8 9]

A =

   1     2     3
   4     5     6
   7     8     9

>>

Once it is executed you will see variable A in workspace.

variable A

The workspace stored the Name, value, size and class of the variable A created.

To edit the value of the variable A in workspace, simply right click and select Edit Value as shown below.

edit value

On you click on Edit Value you will see you can edit the value as shown below −

can edit value

Save and Load Workspace Variables

The workspace is cleared when you exit from matlab. But you can save the workspace in case you want to use it in future.

The file is saved with the .mat extension. When you want it next, open the file in your matlab session.

Here are a few ways to save the workspace variables.

Go to Home tab and you should see the VARIABLE section as shown below

variable section

Click on Save Workspace. It will open the file save dialog window as shown below

save workspace

Save the file with the .mat extension

You can also select a subset of variables in the workspace , right click and click on save selection as shown below −

save selection

Another way to save the full workspace is to right-click on the workspace and click on save workspace as shown below.

save workspace

Since we are done with saving workspace, in this section we will learn how to load the workspace or use the variables present in the workspace.

Here are a few ways to get the variables or load the workspace in matlab.

Inside the Home tab, click on open and select the workspace file you have saved.Click on it and the same will load inside the workspace.

Using load method()

The load() method takes in the filename and you can also specify the variables that you need in it.

Example

load(a)

Here a is the file name a.mat. You dont have to mention the .mat in the method.

When you execute the same in the command window in matlab you will get below output.

You can also specify the variable names you want to load from the a.mat . For example

Consider the file a.mat has variables A, B and C.

If you want to load only A and C you can specify in load method as shown below

load('a', 'A', C)

Using Whos Command

The file content saved can be viewed as shown below.

whos -file a.mat

When you execute in matlab command window the output is as follows −

>> whos -file a.mat
  Name      Size            Bytes  Class     Attributes

  A         1x5                40  double              
  B         1x4                32  double              
  C         1x2                16  double              

>>  

The Bytes tells about the memory used by each variable. Matlab takes care of compressing the data so that less memory is used.

Using Who Command

Similar to whos there is another command who, that displays the names of the variables in workspace.

Example

>>  who

Your variables are:

A  B  C  K  

>>  

Using disp() Method

You can also make use of disp() method, it also takes care of displaying the variables values.

disp(A)

The output on execution is −

>>  disp(A)
    0.8147    0.9058    0.1270    0.9134    0.6324

>> 

Using openvar() Method

This method takes in a variable as the input and the variable is opened inside the variable editor. Later you can change the values or delete them as per your requirement.

openvar(A)

On execution in matlab command window the output is −

matlab command window

Clearing Workspace

With clear workspace it will remove all the variables present in the workspace.By default when the matlab session is closed the workspace is cleared.

But if you still want to clear the workspace when the session is active, here are a few ways to do it.

Inside Home tab, you can make use of clear workspace as shown below.

clear workspace

You can also make use of the command clear inside the matlab command window to clear and remove all items from workspace.

clear  // removes all the variables present in the workspace

clear A  // will only remove the variable A from the workspace

clear -regexp ^test ^a;  // makes use of regular expression and removes all the variables that start with test and a.

Using clearvars Command

This method will remove all the variables present in the workspace. If you specify the variable names only those are removed.

Example

clearvars
clearvars A B C
Advertisements