Unique function cell array conflict

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');
whos
NewParts 2122x21 3330030 cell

 Accepted Answer

It's a cell array but not a cell array of strings.
unique({magic(2),magic(2),rand(2)})
Fail, your error message.
unique({'hello world','what''s up','hello world'})
pass.

11 Comments

All of the cells in the NewParts array are strings except for the NaN cells. Is there a way to have these NaN cells recognized as strings? Also, my goal is to list the unique rows of the NewParts cell array.
idx = cellfun(@(c)all(isnan(c(:))),YourCell);
YourCell{idx} = 'nan'; %nan as a string
For unique rows one way is to horizontally concatenate all of cells together since they're all strings. It would fail in the case of
{'hello ' 'worldTuesday'
'hello world' 'Tuesday'}
Though
I tried the "YourCell" code above, but got the error: ??? The right hand side of this assignment has too few values to satisfy the left hand side.
That must be what you tried to insert it into. There is no left hand side in my above code :)
Isn't "YourCell{idx}" the left hand side of "YourCell{idx} = 'nan';"? For me, YourCell is a list from Excel with strings and NaNs.
Oh yes (doh!), I wasn't seeing the hidden comments. Can you edit the original comments to include the exact code you're using ans a small data set, please?
YourCell = raw_BOM(:,3)
idx = cellfun(@(c)all(isnan(c(:))),YourCell);
YourCell{idx} = 'nan'; %nan as a string
UNIQUE_PARTS = char(unique(YourCell));
YourCell =
'PARTNUM'
[NaN]
'183-1695-010'
'183-1695-010'
'913-3067-150'
'913-3067-150'
'913-3060-080'
...
??? The right hand side of this assignment has too few values to satisfy the left hand side.
Error in ==> Classification at 30
YourCell{idx} = 'nan'; %nan as a string
YourCell =...
{'PARTNUM'
[NaN]
'183-1695-010'
'183-1695-010'
'913-3067-150'
'913-3067-150'
'913-3060-080' };
idx = cellfun(@(c)all(isnan(c(:))),YourCell);
YourCell{idx} = 'nan'; %nan as a string
UNIQUE_PARTS = char(unique(YourCell));
UNIQUE_PARTS =
183-1695-010
913-3060-080
913-3067-150
PARTNUM
nan
No issues...
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)

Sign in to comment.

More Answers (2)

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,:)
Jim Hokanson
Jim Hokanson on 1 May 2013
I just stumbled upon this weird behavior recently. See: http://www.mathworks.com/matlabcentral/answers/74169-unique-rows-cell-array-ignored
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

Categories

Products

Community Treasure Hunt

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

Start Hunting!