Finding structure array entries with certain values

조회 수: 333 (최근 30일)
Tom
Tom 2014년 3월 7일
편집: Thomas 2014년 3월 7일
Hello,
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,
Tom

채택된 답변

Jos (10584)
Jos (10584) 2014년 3월 7일
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.

추가 답변 (1개)

Thomas
Thomas 2014년 3월 7일
You could use something like this
strs={'1' '2' '3' '4'}
ind=find(ismember(strs,'4'))
>> ind =
4
  댓글 수: 2
Tom
Tom 2014년 3월 7일
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...
Thomas
Thomas 2014년 3월 7일
편집: Thomas 2014년 3월 7일
strs in the array of cells.. that I am defining above the find command to give the example..
try
strs ={cel_data.CN};
ind=find(ismember(strs,'4'))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by