Adding variables to workspace window in MATLAB

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

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.

Soumya Srivastava

Soumya Srivastava

Research Analyst at Project Guru
Soumya has completed her MBA in HR from National Institute of Technology (NIT), Kurukshetra and is Bachelor in Information Technology. She loves numbers, their patterns and the infinity nature possessed by them. When it comes to computers, she has interest in memory allocation and virtual memory. In her spare time she likes to read sun signs and personality traits. She believes in hard work and considers smart work to be incomplete without hard work.
Soumya Srivastava

Latest posts by Soumya Srivastava (see all)

Related articles

  • Search and help in MATLAB MATLAB provides various ways in which a user can access help within the software for better understanding of the functions. Similarly, many search options are provided to search text, characters and functions in the software in order to fix errors and avoid bugs.
  • Formatting output and help in MATLAB In the previous articles, the discussion revolved around the use of command window and how variables are added to working window. In the present article, the focus will be on how to format output and further use MATLAB for documentation without errors.
  • Introduction of basic MATLAB interface and quick access tools Matrix Laboratoryn (MATLAB) is a high-level language that is matrix based and includes an interactive environment for numerical computation, visualization,development and programming which was originally written in FROTRAN (Formual Translation). The present module is based on R 2017a […]
  • Matrices and arrays in MATLAB The basics of MATLAB were discussed in the previous module. It covered topics on interface of MATLAB software and the basic operations of MATLAB along the help and documentation features.
  • Handling text value and characters in MATLAB If the text value itself contains single quote; eg. Sam’s in the image 2 below, then three single quotes are used within the statement. Two for text and one for the apostrophe.


We are looking for candidates who have completed their master's degree or Ph.D. Click here to know more about our vacancies.