Sort Cell Array Data
显示 更早的评论
clear all
close
F = xlsread('C:\Users\katie\Desktop\matlabhelp.csv')
t = F(:,1);
time = x2mdate(t,0);
date =datestr(time);
A = [date, F(:,2), F(:,3)];
dn = cellfun(@(x) datenum(x(1:11), 'dd-mmm-yyyy'), A);
[dns,ix] = sort(dn);
FinalSorted = A(ix);
An error occurs saying the following:
Error using cellfun
Input #2 expected to be a cell array,
was char instead.
Error in rough (line 12)
dn = cellfun(@(x) datenum(x(1:11),
'dd-mmm-yyyy'), A);
(Please note data is a small sample)
采纳的回答
You haven’t stated the result you want.
Try this:
[F,S,R] = xlsread('Matlabhelp.csv');
t = F(:,1);
date = datetime(t, 'ConvertFrom','excel')
time = F(:,2)/24;
dv = [datenum(date) + time]
[dv_sort,idx] = sort(dv);
FinalSorted_1 = [datevec(dv_sort) F(idx,3)]
FinalSorted_2 = table(datetime(datevec(dv_sort)), F(idx,3))
FinalSorted_1 =
Columns 1 through 6
2017 12 21 12 1 12
2017 12 21 15 44 24
2017 12 28 23 45 0
2018 1 3 10 35 24
2018 1 16 11 1 12
Column 7
125.51
120.88
776
72.47
67.98
FinalSorted_2 =
5×2 table
Var1 Var2
____________________ ______
21-Dec-2017 12:01:12 125.51
21-Dec-2017 15:44:24 120.88
28-Dec-2017 23:45:00 776
03-Jan-2018 10:35:24 72.47
16-Jan-2018 11:01:12 67.98
9 个评论
I need to sort the dates into chronological order but it still needs to correspond to the same x and y.
I’ve no idea what ‘same x and y’ are.
Note that this sorts the third column of your data to correspond with the first (date) combined with the second (time of day) to create a date-and-time variable, and then uses the idx output of sort with them to assign the appropriate F(idx,3) variables to the output matrix.
The result of my code appears to sort the dates-and-times and data appropriately.
KPsil’s Answer moved here —
I’m sorry I haven’t explained myself very well.
The first column is the date. Second is x Third column is y.
I thought the second column was time.
With that clarification, this works:
[F,S,R] = xlsread('KPSil Matlabhelp.csv');
t = F(:,1);
date = datetime(t, 'ConvertFrom','excel')
% time = F(:,2)/24;
dv = datenum(date)
[dv_sort,idx] = sort(dv);
FinalSorted_1 = [datevec(dv_sort) F(idx,2:3)]
FinalSorted_2 = table(datetime(datevec(dv_sort)), F(idx,2:3))
Thank you so much this works really well.
My pleasure.
If my Answer helped your solve your problem, please Accept it!
I have now manipulated the data and have a new table with the date in form 'dd-mmm-yyyy' in first column and z in second column.
I need to have a cumulative sum of the z for each date, so I can plot the cumulative z score for each date.
e.g 01-Jan-2018 0.10 01-Feb-2018 0.32
See my Answer to your subsequent Question (that I just now posted).
I used your data from your earlier Question, so you will have to revise it slightly to work with your ‘z’ data.
Please could you help with this problem?
https://uk.mathworks.com/matlabcentral/answers/394393-sorting-data-into-different-matrices-which-can-then-be-manipulated?s_tid=prof_contriblnk
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Time Series Objects 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
