MATLAB Answers

How to get 'type' of a variable in matlab?

9.487 views (last 30 days)
Andrew
Andrew on 15 Nov 2013
Commented: Steven Lord 37 minuten ago
Hi, We declare many types of variables in matlab such as double, string, symbolic etc. How to get type of a variable?

  0 Comments

Sign in to comment.

Accepted Answer

Sean de Wolski
Sean de Wolski on 15 Nov 2013
Edited: MathWorks Support Team on 27 Nov 2018
To get the data type, or class, of a variable, use the “class” function.
x = 100;
class(x)
To determine if a variable has a specified data type, use the “isa” function.
tf = isa(x,'double')
For a list of functions that determine if variables have specific attributes, see “is*”.
You also can use the “validateattributes” function, which can validate many classes at once easily.

  0 Comments

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Nov 2013
class(your_variable)

  8 Comments

Show 5 older comments
ANTONIO RUBIA
ANTONIO RUBIA ongeveer 3 uur ago
I think I undesrstod the question because I have a similar problem. Please check this example:
clear all
a = randn(5,1);
b = randn(5,1);
list = who;
Check=cell(2,1);
for j=1:2
Check(j)={class(list{j})}
end
Check is supposed to retrieve programatically the classes of the variables in the workspace (namely, "double" for a nd b). However, it literally reads the class of the argument passed in, i.e., retrieves "char" in both cases. How to programatically get the class of the variables stored, for instance, in the workspace?
Steven Lord
Steven Lord 27 minuten ago
How are you planning to use that information? There are ways to get access to this information (the whos function is one) but depending what you want to do with that information there may be better / safer alternatives.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!