Is it possible to allow the UNIQUE function to support mixed data type cell arrays in MATLAB 7.7 (R2008b)?

2 views (last 30 days)
The UNIQUE function cannot search over cell arrays which contain elements that are not of type string. The following example illustrates this behavior.
C={'abc' 'abc' 4};
unique(C)
??? Error using ==> cell.unique
Input must be a cell array of strings.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to use the UNIQUE function with cell arrays of mixed data types is not available in MATLAB 7.7 (R2008b).
As a workaround, use the CELLFUN command to convert all the elements in the cell array to strings before using the UNIQUE command to retrieve just the unique elements:
A={'abc' 'abc' 4};
Astr=cellfun(@(x)num2str(x), A,'UniformOutput',false)
unique(Astr)
If you have a multidimensional cell array with numeric values in certain columns, and you wish to retrieve unique rows of the cell array, please use the Resolution Document below called 'mixedcellunique.m' as follows:
% Creates an example cell array B
B = cell(5,4);
B{1,1} = 'Henrietta';
B{1,2} = 12;
B{1,3} = 100;
B{1,4} = 'Cherries';
B{2,1} = 'Violet';
B{2,2} = 23;
B{2,3} = 140;
B{2,4} = 'Marshmallows';
B(3,:) = B(1,:);
B(4,:) = B(1,:);
B(5,:) = B(2,:);
% Pulls out just the unique rows of B.
uniqueB = mixedcellunique(B)

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!