MATLAB Answers

How to check whether a column exist in a table?

541 views (last 30 days)
Alon Rozen
Alon Rozen on 24 Nov 2016
Commented: Gabriel Blanco on 25 Jun 2019
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?

  1 Comment

Carlos Aguilar
Carlos Aguilar on 31 Dec 2017
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);

Sign in to comment.

Accepted Answer

KSSV on 24 Nov 2016
Exist_Column = strcmp('My_Column',My_Table.Properties.VariableNames)
val = Exist_Column(Exist_Column==1) ;


Show 2 older comments
Armand Mensen
Armand Mensen on 20 Jun 2017
any(strcmp('My Column', My_Table.Properties.VariableNames))
Nicolas Petit
Nicolas Petit on 31 Aug 2017
ismember('My Column', My_Table.Properties.VariableNames)

Sign in to comment.

More Answers (1)

jkr on 14 May 2019
returns 1 if found, 0 if not


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by