Finding a cell in an array

Hi,
I have a large cell aray called "txt". I am looking for a cell that is in this array, but the location/name changes from time to time. But this cell is always located next to one that has "vehicle" in it. How do i go through the text array and find the "vehicle" cell, but index it so I actually grab the cell next to it since that is the one that I want?
Thanks

Respuestas (3)

dpb
dpb el 8 de Abr. de 2014

1 voto

One way...should be a simpler one but it didn't come to me otomh just now...
c is a cell array around had for an earlier query; just added the word 'vehicle' inside the first location...
>> c
c =
'This is vehicle' 'Description 1234567890'
>> find(cellfun(@(x) ~isempty(strfind(x,'vehicle')),c))+1
ans =
2
>> c(find(cellfun(@(x) ~isempty(strfind(x,'vehicle')),c))+1)
ans =
'Description 1234567890'
>>
Dishant Arora
Dishant Arora el 8 de Abr. de 2014

1 voto

idx = find(cellfun(@(x) isequal(x , 'vehicle') , testCell))+1;

3 comentarios

dpb
dpb el 8 de Abr. de 2014
OP says "...next to one that has "vehicle" in it." which I inferred to mean that isequal may fail. Maybe that means that's the only content, don't know, but otherwise yours and mine are "great minds..." :)
Dishant Arora
Dishant Arora el 8 de Abr. de 2014
Probably you interpreted it right. Let the poster make a call on that.
dpb
dpb el 8 de Abr. de 2014
Ayup...just pointing out the "why" of choosing the apparently more difficult route.
Image Analyst
Image Analyst el 8 de Abr. de 2014
Editada: Image Analyst el 8 de Abr. de 2014

0 votos

Try ismember:
txt = {'aaa', 10; 'bbb', 20; 'vehicle', 30; 'ddd', 40}
% Find 'vehicle' in the first column of the above list of strings.
index = find(ismember(txt(:,1), 'vehicle'))
numberYouWant = txt(index, 2) % Extract second column in that row.

La pregunta está cerrada.

Preguntada:

el 8 de Abr. de 2014

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by