MATLAB: Is it possible to check whether a column exists in a DATASET array

Statistics and Machine Learning Toolbox

I am creating a DATASET array using the Statistics Toolbox v7.2 (R2009b). It contains many columns of data and I would like to be able to query whether a column with a specific label exists in the array.

Best Answer

To check whether a column exists in a dataset array, one can query the variable names and use string comparison on the result. For example,
%%Create Dataset Array
load fisheriris
NumObs = size(meas,1);
NameObs = strcat({'Obs'},num2str((1:NumObs)','%-d'));
iris = dataset({nominal(species),'species'},...
{meas,'SL','SW','PL','PW'},...
'ObsNames',NameObs);
%%Check if column exists
strmatch('SL',get(iris,'VarNames'))