Sort cell array by datetime

129 visualizaciones (últimos 30 días)
Tycho Maas el 23 de Dic. de 2020
Respondida: Eric Sofen el 5 de En. de 2021
I want to sort an array, that looks like the one below, by datetime.
70 26-Nov-2020 10:50:29
84 26-Nov-2020 10:00:00
79.5 26-Nov-2020 14:35:20
53 29-Nov-2020 12:31:00
Any idea how to do this?
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Jemima Pulipati el 26 de Dic. de 2020
Hello,
From my understanding, you want to sort the rows in the cell array according to the datetime values mentioned in the column.
You may use the sort() function which sorts the elements of the input in the order specified.
Here is an example code (sorts in ascending order) using the data you mentioned:
% sample cell array
a = {70, '26-Nov-2020 10:50:29';
84, '26-Nov-2020 10:00:00';
79.5, '26-Nov-2020 14:35:20';
53, '29-Nov-2020 12:31:00'};
[~, idx] = sort(datenum(a(:,2), 'dd-mm-yyyy hh:MM:ss'), 1, 'ascend');
sortedData = a(idx,:);
Here are some answers from the community which might be of relevance to you:
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Tycho Maas el 26 de Dic. de 2020
Thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Eric Sofen el 5 de En. de 2021
Even better, since you're working with data where each column has a different type, consider using a timetable.
t = table2timetable(cell2table(a));
t = sortrows(t);
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by