How can I return indices for specific strings found within cells of a cell array?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kane Fox
am 9 Apr. 2016
Kommentiert: Kane Fox
am 10 Apr. 2016
Hi all!
I have a cell array where each cell has a three letter identification, i.e. 'ABC'
I was wondering if there was a function, or a simple way, of returning indices for specific strings within the cell array, i.e. if I want to find all cells within the array that have 'ABC'. Similar to the find function.
I've written the following function below, and it works, however; it doesn't seem completely efficient.
function Result = pullIndices(charArray, String)
x = 1 ;
Result = NaN(1, length(charArray)) ;
for i = 1:length(charArray)
if strcmp(charArray{i}, String)
Result(x) = i ;
x = x + 1;
end
end
Result = Result(1, 1:x-1) ;
end
Thanks!
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Apr. 2016
pullIndices = @(charArray, String) find( strcmp(charArray, String) );
Single short anonymous function.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!