Azzera filtri
Azzera filtri

find a string from a cell array

6 visualizzazioni (ultimi 30 giorni)
Elysi Cochin
Elysi Cochin il 21 Dic 2016
Commentato: Elysi Cochin il 22 Dic 2016
I have a cell array of dates as
'Mon 22-08-2016'
'Mon 22-08-2016'
'Mon 22-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
if i enter a date as, '23-08-2016', i want to get the first row with that date....

Risposta accettata

José-Luis
José-Luis il 21 Dic 2016
a = {'Mon 22-08-2016'
'Mon 22-08-2016'
'Mon 22-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'}
idx = find(cellfun(@(x) ~isempty(x),strfind(a,'23-08-2016')),1,'first')

Più risposte (1)

Guillaume
Guillaume il 21 Dic 2016
Use a more useful date format, such as datetime
%your inputs:
dates = {'Mon 22-08-2016'
'Mon 22-08-2016'
'Mon 22-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'
'Tue 23-08-2016'};
searchdate = '23-08-2016';
%convert to useful format:
ddates = datetime(dates, 'InputFormat', 'eee dd-MM-yyyy');
dsearchdate = datetime(searchdate, 'InputFormat', dd-MM-yyyy');
find(ddates == dsearchdate, 1) %find first row where dates match
%to display the datetimes with other formats (display won't affect the search
%even if each datetime uses a different format:
ddates.Format = 'eee dd-MM-yyyy'
dsearchdate.Format = 'dd-MM-yyyy'

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by