How to spread daily traffic over the week days?

2 views (last 30 days)
With below code, I am generating graph where average daily traffic of cars with geven time of the day.I also have name of the day for trip travellled in different column. What I want to do is to create weekly traffic of same 24h scale. So, now on x-axis instead of 24h scale, there are weekdays, but 24h within each day. Is there any more straight forward way to do it? Or manipulate splitapply to use timescale along with weekdays? Used data is attached!
load('driving_profile.mat');
% convert start, end times to durations (in minutes)
drivingprofile.st_time = minutes(duration(drivingprofile.st_time,'InputFormat','hh:mm'));
drivingprofile.end_time = minutes(duration(drivingprofile.end_time,'InputFormat','hh:mm'));
drivingprofile.end_time(drivingprofile.end_time==0) = 24*60;
%% Plotting
% To work, the data must be a matrix.
% All data in a matrix must be of the same type, so only extract the necessary variables.
D = table2array(drivingprofile(:,["realdistance","end_time","st_time"])); %realdistance is can plotted against time, value can be changed from data column
%plotTraffic(D);
% format plot
xlim([1 24])
xlabel("Time (h)")
ylabel("Distance travelled (km)")
grid on
% Identify groups and corresponding names
[G,ID] = findgroups(drivingprofile.occupation); % here groups to be analyzed are inserted e.g. occupation group in this case
Y = splitapply(@plotTraffic,D,G);
legend([ID])
function out = plotTraffic(D)
value = D(:,1);
%DURATION OF PARKING AND DRIVING
tt_End = D(:,2);
tt_St = D(:,3);
duration = tt_End - tt_St;
singularvalue = value./duration;
Tzeros_LSH = zeros(24*60+1,1);
traffic = zeros(24*60+1,1);
%% INTERVAL SPREAD
for i = 1:length(tt_End)
TrSt = tt_St(i)+1;
TrEn = tt_End(i)+1;
if isnan(TrEn) || isnan(TrSt)
continue
else
Tzeros_LSH(TrSt:TrEn,1) = Tzeros_LSH(TrSt:TrEn,1) + 1;
traffic(TrSt:TrEn,1) = traffic(TrSt:TrEn,1) + singularvalue(i);
end
end
t_min = 1:length(Tzeros_LSH);
t_hour = t_min/60;
hold on
plot(t_hour,traffic)
%pie(traffic)
hold off
% I get an error message if I don't return something.
out = true;
end

Answers (1)

dpb
dpb on 16 Aug 2020
Good start...looks like could probably simplify the plot routine some and I'd go ahead and create the other variables in the table and resave it instead of doing the same thing over and over, but that's just some refinements...
tdp=drivingprofiletest; % I like short variable names and use t prefix for tables :)
% first go ahead and convert the table variables to those wanted/used...
tdp.st_time=duration(drivingprofiletest.st_time,'InputFormat','hh:mm','Format','m');
tdp.end_time=duration(drivingprofiletest.end_time,'InputFormat','hh:mm','Format','m');
tdp.duration=minutes(tdp.end_time-tdp.st_time);
tdp.hp_id=categorical(tdp.hp_id);
% compute the traffic variable used for plotting
tdp.traffic=tdp.realdistance./tdp.duration;
After that, the table looks similar, but a little different...
head(tdp)
>> head(tdp)
ans =
8×18 table
hp_id wf ef month week_day weekday season st_time end_time duration distance realdistance occupation housing car_size state regiostarGem7 traffic
_______ ____ ______ _____ ________ _______ ______ _______ ________ ________ ________ ____________ __________ _____________________ ________ ______________________ _______________ _______
1001431 2.34 656.00 Mar Mon Weekday Spring 390 min 420 min 30.00 21.85 14333.59 Full-time One/-two family house Compact Schleswig-Holstein Peripheral area 477.79
1001431 2.34 656.00 Mar Mon Weekday Spring 975 min 1005 min 30.00 21.85 14333.59 Full-time One/-two family house Compact Schleswig-Holstein Peripheral area 477.79
1001572 1.05 295.76 Apr Mon Weekday Spring 425 min 460 min 35.00 33.25 9833.98 Part-time One/-two family house Middle North Rhine-Westphalia Urban space 280.97
1001572 1.05 295.76 Apr Mon Weekday Spring 930 min 965 min 35.00 33.25 9833.98 Part-time One/-two family house Middle North Rhine-Westphalia Urban space 280.97
1002821 1.53 427.92 Mar Mon Weekday Spring 900 min 925 min 25.00 11.40 4878.33 Full-time One/-two family house Small Mecklenberg-Vorpommern Peripheral area 195.13
1002822 1.93 540.53 Mar Mon Weekday Spring 350 min 355 min 5.00 0.19 102.70 Full-time One/-two family house Small Mecklenberg-Vorpommern Peripheral area 20.54
1002822 1.93 540.53 Mar Mon Weekday Spring 870 min 895 min 25.00 23.28 12583.51 Full-time One/-two family house Small Mecklenberg-Vorpommern Peripheral area 503.34
1002822 1.93 540.53 Mar Mon Weekday Spring 930 min 955 min 25.00 23.28 12583.51 Full-time One/-two family house Small Mecklenberg-Vorpommern Peripheral area 503.34
>>
Above just preliminaries and some ideas for your consideration...
For the plotting calculations, it appears to me the following code does what your routine does with the possible exception of needing to handle the missing case times -- there were no values in the given dataset that weren't finite--if there are you could remove those from the data before processing instead.
Also, note you don't need to extract data from the table to another temporary array; just reference the variables directly.
t=minutes(0:24*60-1); % a time vector of minutes in 24-hr day
tr=zeros(size(t)); % corollary variable to accumulate sum traffic by time vector
for i=1:height(tFT) % for the elements in the table
i1=tFT.st_time(i);i2=tFT.end_time(i); % start, end indices into vector
tr(i1:i2)=tr(i1:i2)+tFT.traffic(i); % the summation of the traffic value for the time segment
end
I compared the above to your plot for the full-time occupation and it appears to reproduce yours identically.
Now to your subsequent Q? -- yes, just use grouping variables again; except this time group by both occupation and day of week. Your accumulation function can be the same; you'll just then have a pair of grouping variables
[G,ID1,ID2]=findgroups(tdp.occupation,tdp.week_day);
For the to IDs the attached dataset looks like
>> [ID1 ID2]
ans =
9×2 categorical array
Full-time Mon
Full-time Tue
Housewife/-man Mon
Others Mon
Part-time Mon
Part-time Tue
Pensioner Mon
Pensioner Tue
Student Mon
>>
I've gotta' run now, but hopefully that gives a roadmap to follow -- altho may have to think a little more about the actual plot construction since you've essentially got up to seven plots by category for which to display the time traces...

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!