Sorting array by datetimes does not work

Hi,
I need to sort my array, notSorted.mat, by datetimes as strings. The code I use for this is the following:
[~, idx] = sort(datenum(notSorted(:,2), 'dd-MM-yyyy HH:mm'), 1, 'ascend');
sorted = notSorted(idx,:);
However, this does not work. Some values are still not correctly sorted. Does anybody know what went wrong?

Answers (1)

Convert to and sort the table:
T = sortrows(cell2table(HR_date_time_strings),2)

5 Comments

Tycho Maas
Tycho Maas on 4 Jan 2021
Edited: Tycho Maas on 4 Jan 2021
Thanks but this is still not correct. The final rows of the table contain values from November. It currently does the exact same as my code but in stead puts it in a table, so it is still wrong.
Do you maybe know how to fix this?
Right you are.
DT = datetime({HR_date_time_strings{:,2}}','InputFormat','dd-MM-yyyy HH:mm','Format','preserveinput');
T = sortrows(table({HR_date_time_strings{:,1}}',DT(:)),2)
This generates the following error:
Error using ones
Too many input arguments.
Error in matlab.internal.builtinhelper.repmat (line 59)
B = A(ones(siz,'int8'));
Error in table (line 324)
varnames = repmat({''},1,numVars);
Error in CGM_data_04_JAN_2021 (line 149)
T = sortrows(table({HR_date_time_strings{:,1}}',DT(:)),2);
The code runs fine with R2020b. What release are you working with?
It works now, somehow my MATLAB decided that after restarting it worked again...
Thanks!

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 4 Jan 2021

Commented:

on 7 Jan 2021

Community Treasure Hunt

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

Start Hunting!