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).
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).
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.
Saving and clearing the variables
On exiting MATLAB, workspace variables also vanish. To save the workspace variables for future use, use the “save” command.
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.
To load a MAT-file, load command is used in the command window with the file name (See figure 10).
This will display all variables stored in file.mat on the workspace (figure 11)
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.