MATLAB Answers


Finding structure array entries with certain values

Tom 님이 질문을 제출함. 7 Mar 2014
최근 활동 Thomas 님이 편집함. 7 Mar 2014
What is the way to find all structure array entries with specific values?
E.g. I tried:
find(cell_data.CN == 4)
But that returns:
Error using ==
Too many input arguments.
Kind regards,

  댓글 수: 0

로그인 to comment.

답변 수: 2

Answer by Jos (10584)
on 7 Mar 2014
 Accepted Answer

I assume that celldata is a structure array
% create some example data
for k=1:10, celldata(k).CN = ceil(4*rand) ; end
% method 1: easy
allCN = [celldata.CN] % comma separated list expansion
tf1 = allCN == 4
index1 = find(tf1)
% method 2: very flexible
fun = @(x) celldata(x).CN == 4 % useful for complicated fields
tf2 = arrayfun(fun, 1:numel(celldata))
index2 = find(tf2)
Both methods can be written as one-liners.

  댓글 수: 0

로그인 to comment.

Answer by Thomas
on 7 Mar 2014

You could use something like this
strs={'1' '2' '3' '4'}
>> ind =

  댓글 수: 2

What is strs? I tried replacing strs with cell_data.CN there and got
Error using ismember (line 68) Too many input arguments.
So I don't think I understand properly your suggestion...
strs in the array of cells.. that I am defining above the find command to give the example..
strs ={cel_data.CN};

로그인 to comment.

Translated by