我得到了这张表,我想按其中一列中的日期对行进行排序,该表基本上如下所示:
label1 01/12/2020 500 Type2;
label1 26/11/2020 553 Type1;
label2 01/01/2021 951 Type3;
label2 18/12/2019 658 Type3;
label1 01/12/2020 500 Type2;
当我使用 sortrows 时,它按 MM-dd-yyyy 格式对表格进行排序,但我需要将其更改为 dd-MM-yyyy 所以表格最终会是这样的:
label2 18/12/2019 658 Type3;
label1 26/11/2020 553 Type1;
label1 01/12/2020 500 Type2;
label1 01/12/2020 500 Type2;
label2 01/01/2021 951 Type3;
请帮忙解决这个问题
答案 0 :(得分:0)
最好的办法是将时间戳数据转换为 datetime
。在这种情况下,datetime
构造函数可以自动计算出格式,因此您只需:
% Create table
t = table(["label1"; "label1"; "label2"], ...
["01/12/2020"; "26/11/2020"; "01/01/2021"], ...
[500; 553; 951], ...
["Type2"; "Type1"; "Type3"]);
% Convert Var2 into datetime format
t.Var2 = datetime(t.Var2);
% Now use sortrows
sortrows(t, 2)
结果如下
3×4 table
Var1 Var2 Var3 Var4
________ ___________ ____ _______
"label1" 26-Nov-2020 553 "Type1"
"label1" 01-Dec-2020 500 "Type2"
"label2" 01-Jan-2021 951 "Type3"