Open In App

List Variables in Workspace With Sizes and Types in MATLAB

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB works with workspaces, which contain all variables, and their metadata. This makes accessing data in large codes easy. However, sometimes there are requirements for listing all the variables in the MATLAB terminal or properties of some particular variables.  Fortunately, MATLAB provides a method to get this job done; using the whos function.

whos Function

The whos function when called without any argument, lists all the variables in the current workspace with their properties. The syntax of the whos function is:

whos <optional parameters>

we shall see the optional parameters in later sections.

Example 1

Let us see the basic working of whos function.

Matlab




%vector 1
x = 1:10;
%vector 2
y = 3:6.7;
 
% listing all variables with their properties.
whos


Here, we have created two vectors, x, and y. Then, we call the whos function to see how it works.

Output:

 

As we can see, we got the name of the two variables, x, and y, and their size, space was taken in Bytes and data type. If there are some more attributes associated with any variables, they will be displayed in the last section. 

Using the Optional Parameters

In this section, we shall see the usage of optional parameters available with whos function. 

Listing variables that match a REGEX expression

The whos function can be used with REGEX expression. REGEX expressions are standard. the same can be used with the following syntax:

whos -regexp <expression>

Let us see an example of the same.

Matlab




%vector 1
vec_1 = 1:10;
%vector 2
vec_2 = 3:6.7;
%vector 3
x = 3:90;
 
% listing all variables that
% start with vec
whos -regexp vec*


Here, we have 3 variables and we list all the expressions that start with vec. The output will be:

 

Listing Particular Variables 

Now, we can list specific variables using the whos function and pass the variable names as parameters.

whos var1 var2 … varN 

In the following example, we use the same method to list some variables explicitly.

Matlab




%vector 1
vec_1 = 1:10;
%vector 2
vec_2 = 3:6.7;
%vector 3
x = 3:90;
 
whos vec_1 x


This will list the variables vec_1 and x, with their size and other metadata.

Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads