I have a table with two columns first column is the date and x is a value. I need to sum all the x values up for one day and then plot the cumulative summed x for each date.
Does anyone have any suggestions?

 채택된 답변

Star Strider
Star Strider 2018년 4월 9일

0 개 추천

One approach:
[F,S,R] = xlsread('Matlabhelp.csv');
Final = table(F(:,1),F(:,2),F(:,3), 'VariableNames',{'Date','X','Y'});
Final.Date = datetime(Final.Date, 'ConvertFrom','excel', 'Format','dd-MMM-yyyy')
[G,TID] = findgroups(Final.Date);
FinalSummed.Date = TID;
FinalSummed.X = splitapply(@sum, Final.X, G);
FinalSummed.Y = splitapply(@sum, Final.Y, G);
FinalSummed = struct2table(FinalSummed)
producing first:
Final =
5×3 table
Date X Y
___________ _____ ______
03-Jan-2018 10.59 72.47
16-Jan-2018 11.02 67.98
28-Dec-2017 23.75 776
21-Dec-2017 15.74 120.88
21-Dec-2017 12.02 125.51
then:
FinalSummed =
4×3 table
Date X Y
___________ _____ ______
21-Dec-2017 27.76 246.39
28-Dec-2017 23.75 776
03-Jan-2018 10.59 72.47
16-Jan-2018 11.02 67.98

댓글 수: 4

KPSil
KPSil 2018년 4월 9일
Thank you this works well with my data however, the z values are summed within their dates and I would like the values to be summed chronologically as i.e date1=date1, date2=date1+date2, date3=date1+date2+date etc.
Do you have any advice on how to do this?
I would just use the cumsum function, for example:
FinalSummed = struct2table(FinalSummed)
FinalSummed.X = cumsum(FinalSummed.X)
FinalSummed.Y = cumsum(FinalSummed.Y)
This works, and appears to produce the correct result. Those additional lines go after the code I posted earlier (and the reason I included the struct2table call for reference).
KPSil
KPSil 2018년 4월 9일
When I plot the data I only get a graph with the months on not the individual days within the months.
How do I specify the days?
Star Strider
Star Strider 2018년 4월 9일
See the documentation on xticklabels (link) and xtickformat (link). You might also need the xtickangle (link) function to rotate them to be readable.
In my experience, the axis date function results have proven to be difficult to modify. You will likely have to experiment to get the result you want.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 4월 11일

0 개 추천

In R2016b or later, timetables make the "compute daily sum" step super easy:
tt2 = retime(tt,'daily','sum')

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

태그

질문:

2018년 4월 9일

답변:

2018년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by