Table with date and x need to plot graph

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?
Star Strider
Star Strider 2018 年 4 月 9 日
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 ExchangeData 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