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)

 Accepted Answer

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 Comments

KPSil
KPSil on 8 Apr 2018
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))
KPSil
KPSil on 9 Apr 2018
Thank you so much this works really well.
My pleasure.
If my Answer helped your solve your problem, please Accept it!
KPSil
KPSil on 9 Apr 2018
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.
KPSil
KPSil on 14 Apr 2018
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

Sign in to comment.

More Answers (0)

Products

Asked:

on 8 Apr 2018

Commented:

on 14 Apr 2018

Community Treasure Hunt

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

Start Hunting!