getting data types of table

113 ビュー (過去 30 日間)
Callum Clarke
Callum Clarke 2016 年 9 月 27 日
編集済み: Leon 2024 年 2 月 19 日
I want to compare data types of each column of a table against a cell array of column header-data type pairs, with the end goal of replacing columns with non-matching data types with the correct ones.
What I have managed so far is a loop that creates another cell array of header-data type pairs. What I need now is a way to compare this array to the 'checklist' array, returning which columns have non-matching data types. There is no guarantee of the table having the same columns or being on the same order as the array it is checked against. I tried ismember() but it just returns a 2xn array of 1s, even for tables that shouldn't match. I think I need to define key/value pairs somehow.
Any suggestions on how to do this, or other methods entirely, are more than welcome!
  1 件のコメント
dpb
dpb 2024 年 1 月 1 日
Agree, this should be a metadata property of the table and thus directly accessible.

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

採用された回答

Peter Perkins
Peter Perkins 2016 年 10 月 3 日
編集済み: Cris LaPierre 2022 年 11 月 18 日
It seems like you want a list of the table's variable names, a list of the variable's types, and your 2xn cell array of names/types. Use t.Properties.VariableNames and varfun(@class,t,'OutputFormat','cell') to get the first two things.
Then it seems like you'd want to use ismember )both outputs) on the variable names to figure out which are matched in your other list, and where, and then for the ones that do match, compare their types using strcmp.
Hope this helps.
  2 件のコメント
dpb
dpb 2024 年 1 月 1 日
@Peter Perkins -- I never could figure out why this isn't a member of the table properties stuct, just like .VariableNames; seems like it would be obvious to want/need on occasion; particularly if it is needed to be able to clone a table if one wishes to augment an existing one with some more data.
It goes along with my previous wishes for a function that would append an empty record to an existing table...
Leon
Leon 2024 年 2 月 19 日
編集済み: Leon 2024 年 2 月 19 日
I third that. I'd like this feature too. And I also recently asked about how to add an empty record too! This may be of interest for that:
Did you find a better way?

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

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2016 年 9 月 27 日
Callum - class allows you to determine the class or data type of an object. For example,
>> x = 42;
>> class(x)
ans =
double
This function may be useful in determining the data type of each object in your table.
  1 件のコメント
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 11 月 30 日
Thank you Geoff!
The suggested command works also with table variables.
Best!

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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by