MATLAB Answers

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

9,870 views (last 30 days)
Andrew
Andrew on 15 Nov 2013
Commented: Steven Lord on 21 Feb 2020
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)

  3 Comments

Robert Bruner
Robert Bruner on 17 Jul 2019
"Error: the class function must be called from a class constructor"
how can I do this from the command-line in MATLAB?
Bill Tubbs
Bill Tubbs on 20 Feb 2020
I have the same problem.
>> class(theFiles)
ans =
'struct'
>> class(theFiles.name)
Error using class
The CLASS function must be called from a class constructor.
Steven Lord
Steven Lord on 21 Feb 2020
I'm guessing theFiles is a non-scalar struct array, which would mean that theFiles.name is a comma-separated list. This makes MATLAB think you're using a different syntax for the class function, one associated with the older style of classes. See the "Obsolete Class Definition Syntax" section on the class documentation page.
You could try:
class(theFiles(1).name)
class(theFiles(2).name)
% etc

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!