Finding all occurences of a string within a cell array (itself part of a struc)

1 次查看(过去 30 天)
I have the following structure
dataDens =
dens: [1x172 double]
level: {1x172 cell}
raga: {1x172 cell}
within which dataDens.raga consists of (reducing the number of columns below for simplicity)
Columns 1 through 3
'Multani' 'Tori' 'Tori'
I'd like to find the indices at which 'Tori' appears (that is, [2 3] for the example above). However, all of the commands I tried (below) either give an error, or return blank outputs.
I think it's probably just a matter of adding/removing a curly bracket somewhere, or using some conversion; but I am at my wit's end, and hope someone can help clarify - thanks in advance!
indices = find(strcmp([dataDens.raga{:}], {'Tori'}))
indices = ismember('Tori', dataDens.raga)
[if,where] = ismember('Tori', dataDens.raga)

采纳的回答

Jan
Jan 2019-4-26
编辑:Jan 2019-4-26
match = strcmp('Tori', dataDens.raga) % Logical indexing
index = find(match) % Linear indexing
If you want to obtain the correspodning elements from the other fields, logical indexing is efficient:
dataDens.dens(match) % Usually faster than: dataDens.dens(index)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by