How to get 'type' of a variable in matlab?
Mostrar comentarios más antiguos
Hi, We declare many types of variables in matlab such as double, string, symbolic etc. How to get type of a variable?
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 15 de Nov. de 2013
class(your_variable)
9 comentarios
Robert Bruner
el 17 de Jul. de 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
el 20 de Feb. de 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
el 21 de Feb. de 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
Mahalakshmi Sivakumar
el 3 de Mzo. de 2021
how to use this class inside .m script it throws a char but i want parameter
Steven Lord
el 3 de Mzo. de 2021
I don't understand your question. Could you explain what you mean by "it throws a char" and "i want parameter" in a little more detail?
ANTONIO RUBIA
el 8 de Mzo. de 2021
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
el 8 de Mzo. de 2021
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.
ANTONIO RUBIA
el 8 de Mzo. de 2021
Dear Steven, thank you for your constructive and useful answer. My plan is to use that information in an app environment similar to the Import Data functionality of the Econometric Modeler app. The point is to allow the user to import data from the workspace into the main app by selecting the variables listed on a table by rows, with columns showing different information (name, class, size, etc.).
The function whos you suggested does the job. In my case, something like:
info = evalin('base', 'whos')
collects all the information meant to be reported in the selection table and can be processed programatically in a function. Your suggestion should help clarify M. Sivakumar's earlier question too.
This is already a valid solution, but in case you may have better /safer alternatives, I would definitively love to know more about them.
Thanks !
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!