Hi all,
I search for this question in the web and found the following solution: Suppose I have a table named 'My_Table' and want to check if it contains a column (field) named 'My_Column'. The code:
Exist_Column = strmatch('My_Column',My_Table.Properties.VariableNames)
should result in logical that answer the question. I checked it and it works. However, the Matlab editor commented that 'strmatch' is not a recommended function to use because it is about to be removed in future version and suggest instead the use of either 'strncmp' or 'validatestring'. Problem is that both do not return a logical and more steps are required to get the answer.
So, my question is: what is the simplest recommended way to determine if a certain column name exist in a table?
Thanks,
Alon

1 Kommentar

Just came across to this question. An anonymous function will also do the job (and will be reusable within the scope):
isTableCol = @(t, thisCol) ismember(thisCol, t.Properties.VariableNames);

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

KSSV
KSSV am 24 Nov. 2016

2 Stimmen

Exist_Column = strcmp('My_Column',My_Table.Properties.VariableNames)
val = Exist_Column(Exist_Column==1) ;

6 Kommentare

Alon Rozen
Alon Rozen am 24 Nov. 2016
Thanks a lot KSSV - you are a treasure :)
Pity that there is no simple, one command, way to do that. I think MathWorks should consider to add something like 'isfield' for that purpose.
Thanks again,
Alon
KSSV
KSSV am 24 Nov. 2016
There might be other ways ..will let you know..
any(strcmp('My Column', My_Table.Properties.VariableNames))
ismember('My Column', My_Table.Properties.VariableNames)
Bonjour Nic!
any("MyColumn" == string(My_Table.Properties.VariableNames))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

jkr
jkr am 14 Mai 2019

4 Stimmen

sum(ismember(mx.Properties.VariableNames,'NameYouWantToCheck'))
returns 1 if found, 0 if not

2 Kommentare

AusPollen
AusPollen am 23 Mai 2021
Nice! although you don't need to sum if you switch the ismember arguments, ie. use ismember('My_Column',My_Table.Properties.VariableNames)
This is exactly what I wanted. Thanks a lot for this trick!!

Melden Sie sich an, um zu kommentieren.

Sean Lynch
Sean Lynch am 5 Sep. 2021

0 Stimmen

I used "any", but agree "ismember" is even better
exists = any(strcmp('Column-Name', My_Table.Properties.VariableNames))

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by