find a string from a cell array

2 次查看(过去 30 天)
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....

采纳的回答

José-Luis
José-Luis 2016-12-21
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')

更多回答(1 个)

Guillaume
Guillaume 2016-12-21
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'

标签

Community Treasure Hunt

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

Start Hunting!

Translated by