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?

 Akzeptierte Antwort

Star Strider
Star Strider am 9 Apr. 2018

0 Stimmen

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 Kommentare

KPSil
KPSil am 9 Apr. 2018
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 am 9 Apr. 2018
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 am 9 Apr. 2018
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 11 Apr. 2018

0 Stimmen

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

Kategorien

Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by