MATLAB Answers


Unique function cell array conflict

Asked by Brian
on 28 Jun 2011

Matlab is giving me a seemingly conflicting error message. It says that the 'rows' flag is ignored since NewParts is a cell array, however it says that it cannot perform the unique function since NewParts is not a cell array. whos says that NewParts is a cell array. Are there any ways to resolve this conflict?

>> Classification
Warning: 'rows' flag is ignored for cell arrays. 
> In cell.unique at 31
  In Classification at 103
??? Error using ==> cell.unique at 45
Input must be a cell array of strings.
Error in ==> Classification at 103
UNIQUE_NewParts = unique(NewParts,'rows');
NewParts                 2122x21            3330030  cell


3 Answers

Answer by Sean de Wolski
on 28 Jun 2011
 Accepted answer

It's a cell array but not a cell array of strings.


Fail, your error message.

unique({'hello world','what''s up','hello world'})



YourCell =...
'913-3060-080' };

idx = cellfun(@(c)all(isnan(c(:))),YourCell);
YourCell{idx} = 'nan'; %nan as a string
UNIQUE_PARTS = char(unique(YourCell));



No issues...

on 29 Jun 2011

Is your [NaN] actually Not A Number? Or is it a string or something? The real problem is that it comes from a blank cell in Excel.

yes, my nan is _actually_ a nan. I convert it to a string that says nan but it is no longer a nan, it's a string (so unique can be called)

Answer by Brian
on 28 Jun 2011

Thanks for your help Sean de. Since the first column was driving the unique parts, I used this solution.

[B,I,J] = unique(NewParts(:,1));
UNIQUE_NewParts = NewParts(I,:)


Answer by Jim Hokanson
on 1 May 2013

I just stumbled upon this weird behavior recently. See:

In summary, a warning is thrown saying that unique will not work with the rows option, but then it proceeds perform unique on all elements anyways, at which point in time it fails due to type inconsistencies.

For others finding this answer, I've submitted a more general FEX submission a few years ago to address this problem: 25917: Unique Rows for a Cell Array


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!