MATLAB Answers


How to obtain the filtered Index of Cell Array

Asked by Kushan
on 6 May 2013
% This is the code
AllNames= {'A.1';'A.1';'A.10';'A.10';'A.10';'A.10';'A.15';'A.15';'A.16'	;'A.17';'A.17';'A.17';'A.17';'A.17';'A.20A';'A.20A';'A.20A';'A.20A'};
ide_names=unique(AllNames(1:end,1)) ;
for i= 1:length(ide_names)

% I want to create a new data struct(data) using the filtered Index from Cell Array A with values contain in B column vector. However, Once I ran the code below error is poping up.

_Undefined function 'eq' for input arguments of type 'cell'.

Error in a (line 6) index=find(AllNames(:,1)==ide_names(i,1));_

% could someone help me out, please

  1 Comment

Jan Simon
on 6 May 2013

I have formatted your code. It is easy: mark it with the mouse, hit the "{} Code" button, care for a blank line before and after the code.


No products are associated with this question.

3 Answers

Answer by Andrei Bobrov
on 6 May 2013
Edited by Andrei Bobrov
on 6 May 2013

AllNames= {'A.1';'A.1';'A.10';'A.10';'A.10';'A.10';'A.15';'A.15';'A.16' ;'A.17';'A.17';'A.17';'A.17';'A.17';'A.20A' ;'A.20A';'A.20A';'A.20A'};
[a,c,c] = unique(AllNames); %[EDIT}
bb = accumarray(c,B,[],@(x){x});
data = struct('Name',a,'Value',bb);


Answer by Kushan
on 6 May 2013

Thanks a lot for your quick response. But below line is not working

[a,~,c] = unique(AllNames,'stable');

The below error massage is popping up

 [a,~,c] = unique(AllNames,'stable');
Error using cell/unique (line 28)
Unrecognized option.

Please help me out

  1 Comment

See EDIT part in my answer.

Answer by Jan Simon
on 6 May 2013

You can change the line:

index=find(AllNames(:,1)==ide_names(i,1));  % ERROR


index = strcmp(AllNames(:,1), ide_names{i,1});

But using the outputs of unique is faster.


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!