Filter löschen
Filter löschen

find a string from a cell array

6 Ansichten (letzte 30 Tage)
Elysi Cochin
Elysi Cochin am 21 Dez. 2016
Kommentiert: Elysi Cochin am 22 Dez. 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....

Akzeptierte Antwort

José-Luis
José-Luis am 21 Dez. 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')

Weitere Antworten (1)

Guillaume
Guillaume am 21 Dez. 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'

Kategorien

Mehr zu MATLAB 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!

Translated by