Exporting variable names and their respective data types to excel

4 views (last 30 days)
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 Comment
Adam Danz
Adam Danz on 12 Jan 2021
Edited: Adam Danz on 12 Jan 2021
> 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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Jan 2021
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!