Adding variables to workspace window in MATLAB

By Soumya Srivastava on July 5, 2017

As discussed in the previous article, workspace is a space wherein the variables used and created in a study are displayed. The variables can also be imported from data files or other programs. This article explains the process of adding variables to MATLAB and assigning values to those variables.

For example, we have to assign values to 2 variables i.e. “a”, and “b”, wherein values assigned are 4, 2 respectively. Then when a operation is performed between a and b i.e. “c=a-b” then value for “c” is also defined in the command window as shown in figure below.  The assigned values for variables “a” and “b” and computed value for variables “c” is then displayed in workspace window automatically as shown in figure 2 (See below).

adding variables and assigning the values to them
Figure1: Adding variables and adding values in Command Window
displaying of the values of the variables added in the first section
Figure 2: Workspace window displaying variables created and used in the command window along with their assigned values

Furthermore, along with the numerical variables, string values can also be assigned to a variable (which is this case is “d”) as shown in figure 3 and the value i.e. “Hello” for the variable is displayed in the workspace window automatically (Figure 4).

adding the string to the variable using the command window
Figure 3: Assignment of a string to a variable in Command Window
displaying the variable in the command window after its creation in the previous section
Figure 4: A variable being displayed with string value in Workspace window after being created in Command Window

Determining the size of variables

In order to determine the size of all variables (i.e. a, b, c and d) being displayed in the workspace (see figure below), you need to type “whos” and press “Enter” in the command window. Size of the variables will be displayed in the command window itself as shown in Figure 6.

workspace window displays the variable
Figure 5: Variables displayed in Workspace Window
using the whos command to display the variable in workspace window
Figure 6: Size of the variables displayed in workspace window obtained in command window using whos command

Saving and clearing the variables

On exiting MATLAB, workspace variables also vanish. To save the workspace variables for future use, use the “save” command.

using the save command and saving the file in the Mat format
Figure 7: Save command used to save variable file named file.mat

In figure 7, save command is used to save all the variables of workspace (figure 5) with name file and extension “.mat” which represents a MAT- file.

To clear the workspace window, use clear command in command window.

using the clear command to clear the displayed variables in the workspace window
Figure 8: Clear command used clear the variables displayed in workspace window
showing the empty workspace window after using the clear command
Figure 9: Empty workspace window after using clear command

To load a MAT-file, load command is used in the command window with the file name (See figure 10).

using the loading command to reload the variables in the existing workspace
Figure 10: Command load is used to reload the variables of a file named load.mat

This will display all variables stored in file.mat on the workspace (figure 11)

reloading the variables files in the workspace
Figure 11 – Reloaded variables in workspace of a file named file.net

Now the process of adding variables in MATLAB is clear, the next article will focus on text and characteristics. Furthermore the next article will also include the process of assigning the numeric values to string variables.

Discuss