Sort cell array by datetime
114 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Jemima Pulipati
2020 年 12 月 26 日
Hello,
From my understanding, you want to sort the rows in the cell array according to the datetime values mentioned in the column.
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 件)
Eric Sofen
2021 年 1 月 5 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!