MATLAB Answers

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

6.802 views (last 30 days)
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?


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;
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.


Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Nov 2013


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 =
>> class(
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 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:
% etc

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!