Borrar filtros
Borrar filtros

find a string from a cell array

6 visualizaciones (últimos 30 días)
Elysi Cochin
Elysi Cochin el 21 de Dic. de 2016
Comentada: Elysi Cochin el 22 de Dic. de 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....

Respuesta aceptada

José-Luis
José-Luis el 21 de Dic. de 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')

Más respuestas (1)

Guillaume
Guillaume el 21 de Dic. de 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'

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by