Effacer les filtres
Effacer les filtres

Check elements in cell

95 vues (au cours des 30 derniers jours)
Palumbo Piero
Palumbo Piero le 22 Nov 2017
Commenté : Drewsky3 le 26 Sep 2019
Hello, I have this cell:
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] 'Del Col_2010' [] [] [] [] 'Lee&Mudawar_2005'
and I have written this code:
for z=1:length (NAME_T)
if isequal(NAME_T,'Del Col_2010')
disp ('YES')
else
disp ('NO')
end
end
But the answer is always NO. What can i do? THanks in advance.

Réponse acceptée

KL
KL le 22 Nov 2017
Modifié(e) : KL le 22 Nov 2017
No need for a loop, just use find and strcmp,
indx= find(strcmp(NAME_T, 'Del Col_2010'))

Plus de réponses (1)

dpb
dpb le 22 Nov 2017
The line if isequal(NAME_T,'Del Col_2010') inside the loop doesn't help because you don't index the cell array; NAME_T returns the full cell array, not individual elements within; you were looking for NAME_T(z) to look at individual cells although there the regular parentheses return the cell, not the content of the cell; that would be done via "the curlies" as if NAME_T{z}.
for z=1:length (NAME_T)
if isequal(NAME_T{z},'Del Col_2010')
disp ('YES')
else
disp ('NO')
end
end
The string functions aren't as flexible as could be when all the cell array content is character; you have to use some roundabout ways to do the search with vectorized Matlab functions--
>> c={[] [] [] 'Del Col_2010' }; % a short example of case for demonstration
>> strfind(c,'Del Col_2010')
Error using cell/strfind (line 32)
If any of the input arguments are cell arrays, the first must be a cell array of strings and the second must be a character array.
>> regexp(c,'Del Col_2010') % and another...
Error using regexp
All cells must be strings.
>>
Well, pooh! What can we do????
>> cellfun(@(c) strfind(c,'Del Col_2010'),c,'uniform',0) % cellfun is essentially the loop
ans =
[] [] [] [1]
>> cellfun(@isempty,ans) % this what we want except opposite sense; the location is false
ans =
1 1 1 0
>> cellfun(@(c) strfind(c,'Del Col_2010'),c,'uniform',0) % repeat
ans =
[] [] [] [1]
>> find(~cellfun(@isempty,ans)) % look for logical FALSE instead of TRUE...
ans =
4
>>
That's the element we wanted...so to put it all together,
>> find(~cellfun(@isempty,cellfun(@(c) strfind(c,'Del Col_2010'),c,'uniform',0)))
ans =
4
>>
Aha!!! That's the cell location trying to find. To make this more useful, create a dynamic function handle the embeds the desired string in it and then use that function in the inner call rather than the explicit string as shown above.
There really should be a simpler syntax and perhaps there is but I've never found it...
  1 commentaire
Drewsky3
Drewsky3 le 26 Sep 2019
Hi, I have a very similar problem but am getting mixed up with cells, characters, etc. I have a cell arry t_stamp where the contents are strings of date-time.
I want to find a specific date-time and know what row that is in, to use it as the starting point of a for loop.
Eg:
t_stamp=
'2019-09-18 10:12:59 AM'
'2019-09-18 10:13:00 AM'
'2019-09-18 10:13:01 AM'
then I want to find the row that '2019-09-18 10:13:01 AM' is in:
index=strfind(t_stamp, '2019-09-18 10:13:11 AM')
This gives a cell arry as:
index =
[]
[]
1
how do i then define the row of 1 as istart?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by