calling unique function on a string column of a table failed
显示 更早的评论
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 个评论
Stephen23
2023-5-1
[v, u] = unique({T.C})
% ^ ^ get rid of these
Enping
2023-5-2
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
2023-5-2
@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
2023-5-2
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!