calling unique function on a string column of a table failed
Show older comments
I have a simple table (say, T) one column (say C) of which is a string column. [v, u] = unique({T.C}) fails with the exception:
Cell array input must be a cell array of character vectors.
7 Comments
Stephen23
on 1 May 2023
[v, u] = unique({T.C})
% ^ ^ get rid of these
Enping
on 2 May 2023
Probably related, the existing script also has code like T(i).C. Now such code causes the error "Dataset array subscripts must be two-dimensional."
You have the ability to demonstrate that for us here directly by attaching T in a .mat file to your post and running the line of code that produces the error. That will allow us all to examine exactly what is happening. It doesn't really make sense that you are getting error messages referring to T as a "dataset" variable when in your OP, you said T was a table.
The dataset class is a table-like class, the precursor for table arrays, introduced in Statistics and Machine Learning Toolbox in release R2007a. [We recommend using table arrays instead of dataset arrays and have for several releases.] It behaves much like a table array and so colloquially calling it a "table" seems reasonable (even though technically it's not a table.)
The equivalent error for a table array would be (using try / catch and warning so I can run further code in the comment):
T = array2table(magic(4));
try
y = T(1)
catch ME
warning(ME.message)
end
I don't think this type of indexing operation ever worked for either table or dataset.
In general, if you're working with string data you should operate on string arrays rather than cell arrays containing string arrays.
nephews = ["Huey"; "Dewey"; "Louie"] % Preferred
nephewsC = {"Huey"; "Dewey"; "Louie"}
Enping
on 2 May 2023
@Enping this is now a very different question, having to do with dataset objects and nothing to do with table objects and unique() as you originally posted. Therefore, you should ask about dataset object indexing in a different thread, hopefully after accept-clicking the answer I have put below, which does address your original question.
Enping
on 2 May 2023
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!