Computing the number of variables belonging to each data type

2 ビュー (過去 30 日間)
chiefjia
chiefjia 2021 年 5 月 5 日
コメント済み: chiefjia 2021 年 5 月 6 日
Hi,
I'm a real beginner in the programming world and also in MATLAB. I'm trying to calculate how many variables (columns from the table) belong to the data types "cell" and "double". So far, I have managed to find what type of data is each column/variable through the following function:
var_class = varfun(@class,M,'OutputFormat','table')
However, this function just gives me the type of data that each variable contains, but not how many variables/columns there are of each type of data. I have tried to calculate this through a for-loop that computes how many 'double' and 'cell' strings there are in the table, by consistently updating the number in the array "total_dtype". The first element would refer to the # of columns containing 'cell' data types and the second element the # of columns containing 'double' data types. I haven't had any success yet. This is what i've come up with for now:
total_dtype = [0 0]
for col=1:width(var_class)
if var_class{:,1} == 'double'
total_dtype(1,1) = total_dtype(1,1) + 1
else
total_dtype (1,2) = total_dtype(1,2) + 1
end
end
I would really appreciate your help.
Thank you in advance,
Jon

採用された回答

dpb
dpb 2021 年 5 月 5 日
How about
n_cl=countcats(categorical(varfun(@class,M,'OutputFormat','cell')))
  1 件のコメント
chiefjia
chiefjia 2021 年 5 月 6 日
Thank you so much! This works :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by