MATLAB Answers

How to create a script in Matlab

3 views (last 30 days)
gjashta on 11 May 2019
Edited: gjashta on 14 May 2019
I have the following codes: I want to bin the price data in 4 bins per month and to calculate the mean and std of demand that belong in each bin by month and year.
Can you help me to create a function that contain (the bin, month, year) as a script?? I am repeating the same code for each month, I need a structure that calculate
them very fast and save all the results in one table.
Price = Data1(:,1 );
Demand = Data1(:,2 );
tv = (datetime([2006,1,1]):datetime([2018,12,31])).';
[Ye,M,D] = ymd(tv);
DT = timetable(Price,Demand, Ye, M, D, 'RowTimes', tv );
MarchDataPD = DT(DT.M==3,{'Price','Demand'});
binEdges1=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(1:31),binEdges1);
for i = 1:NumBins
binEdges2=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(32:62),binEdges2);
for s = 1:NumBins

Accepted Answer

Guillaume on 11 May 2019
It's a bit puzzling that you are aware of timetables and yet resort to deprecated functions such as histc (which has no concept of dates) for the rest of your code, instead of using timetables functions which would make your task trivially easy.
Even the way you construct your timetable is more complicated than it should:
%Data1: a 2 column matrix of price and demand:
DT = array2timetable(Data1, 'VariableNames', {'Price', 'Demand'}, 'RowTimes', datetime(2006, 1, 1):dateime(2018, 12, 31));
There is no point in storing Ye, M, D separately. It's already stored as part of the time.
Once you have a timetable, you can easily resample it at regular or irregular intervals using retime. However retime doesn't do
%DT: a timetable
meanDT = retime(DT, 'regular', 'mean', 'TimeStep', caldays(14)); %discussion about timestep coming up
stdDT = retime(DT, 'regular', @std, 'TimeStep', caldays(14));
meanDT.Properties.VariableNames = {'MeanPrice', 'MeanDemand'};
stdDT.Properties.VariableNames = {'StdPrice', 'StdDemand'};
meanstdDT = [meanDT, stdDT]
Instead of using two retime, you could use groupsummary instead:
meanstdDT = groupsummary(DT, 'Time', caldays(14), {'mean', 'std'})
Now you say you want 4 bins per month. What does that mean exactly. 4 equal bins per month means that the edges fall at odd times during the days instead of falling at the end of days. That doesn't sound like something you'd want. In the above, I've used bins of 14 days but both retime and groupsummary support a bin vector instead. To construct a bin vector with 4 equal bins per month, the simplest is probably:
monthvector = DT.Time(1) : calmonths(1) : DT.Time(end) + calmonths(1);
binvector = fillmissing(reshape([monthvector; NaT(3, numel(monthvector))], [], 1), 'linear')
and then
meanstd = groupsummary(DT, 'Time', binvector, {'mean', 'std'})
gjashta on 14 May 2019
Thank you Guillaume! And sorry because as a beginner in using Matlab, I don't express the question correctly.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!