Exporting variable names and their respective data types to excel

4 次查看(过去 30 天)
Hi,
I have many tables with different sizes. I am trying to concatenate these tables where the column names match but it seems that their datatypes are different which is yelding errors. Is there a way to export variable names and their respective datatypes of each table to excel in order to see which variables don't have the same data type and change them accordingly?
In other word. I have Matrix M with size of 10,000 x 500. How can I get the datatype of each column in excel?
  1 个评论
Adam Danz
Adam Danz 2021-1-12
编辑:Adam Danz 2021-1-12
> Is there a way to export variable names and their respective datatypes of each table to excel in order to see which variables don't have the same data type and change them accordingly?
This suggests that the data are already in Matlab. To get the class of each variable of a table, use,
varClass = varfun(@class,T); % T is a table
There are function that can cast a variable class to a new class such as num2str(), categorical(), etc.
If the Matlab data were created by reading data from a file, fix the problem by reading in the data correctly by specifying the intended class which can be done in all of the tabular read-in functions such as readtable and readcell.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2021-1-12
You can get the MATLAB datatypes by using
vt = reshape(varfun(@class, YourTable, 'outputformat', 'cell'), [], 1);
[uvt, ~, g] = unique(vt)
counts = accumarray(g(:), 1);
[uvt, num2cell(counts)] %displays types and counts
[~, mcidx] = max(counts);
not_common_idx = find(g ~= mcidx);
[num2cell(not_common_idx(:)), vt(g(not_common_idx)) %displays variable numbers and type for all but most common

标签

Community Treasure Hunt

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

Start Hunting!

Translated by