How can I find unique cells in a table in MATLAB R2014a that contains non-string cell arrays?

20 views (last 30 days)
I'm trying to run "unique" on a table object that contains cells that are not strings (one column in my table contains mixed size numerical matrices), but I ran into an error message saying that table/unique() only works for cell arrays of strings but not cells in general. How can I find unique rows in this table in MATLAB R2014a?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Mar 2014
The "unique" function for the table class in MATLAB R2014a does not support columns that contain cell arrays that are not cell arrays of strings. This is because under-the-hood the "unique" function for tables uses the "unique" function for cell arrays, which itself does not support non-string cell arrays.
To work around this limitation, you need to temporarily replace any columns in your table that contain non-string cells with replacement columns that contain unique placeholder integers, one for each unique observation in the individual columns.
To do this, you first need a "unique" function that will work on individual non-string cell arrays. There are several options for performing a "unique" operation on non-string cell arrays, including several entries on MATLAB Central File Exchange. Attached is one option, called "uniqueCellVector", which uses a brute force method that compares every element in the cell array with every other element.
What is important is that the "unique" function you use returns three output arguments using the same syntax as the built-in "unique" function.
Once you have done this replacement, you can use "unique" on the table as normal, then substitute your original values back into the new table.
To summarize this approach:
>> strdata = {'a','b','c','a','b','c'}';
>> numdata = [1 2 3 1 2 3]';
>> celldata = {1; [2 3]; [4 5 6]; 1; [2 3]; [4 5 6]};
>> A = table(strdata,numdata,celldata)
>> [C,ia,ic] = uniqueCellVector(A.celldata);
>> A.celldata = ic;
>> B = unique(A);
>> B.celldata = C(B.celldata)
This approach should work no matter how many non-string cell values you have in your table. You would just need to repeat the substitution process on each column of data that is not either numerical or string data.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!