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

6,173 ビュー (過去 30 日間)
Andrew
Andrew 2013 年 11 月 15 日
コメント済み: ANTONIO RUBIA 2021 年 3 月 8 日
Hi, We declare many types of variables in matlab such as double, string, symbolic etc. How to get type of a variable?

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 11 月 15 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
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.

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 15 日
class(your_variable)
  9 件のコメント
Steven Lord
Steven Lord 2021 年 3 月 8 日
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
ANTONIO RUBIA 2021 年 3 月 8 日
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 !

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by