How do I plot by categroy group?
조회 수: 6 (최근 30일)
이전 댓글 표시
I am trying to generate graph of traffic by each occupation group of car users. So far, I can produce overall traffic with the code below:
load('driving_profiletest.mat');
drivingprofiletest.end_time(strcmp(drivingprofiletest.end_time,'00:00')) = {'23:59'};
drivingprofiletest = convertvars(drivingprofiletest, {'st_time', 'end_time'}, 'string');
%% DATA VARIABLES
ev_id = drivingprofiletest.hp_id;
distance = drivingprofiletest.distance;
realdistance = drivingprofiletest.realdistance;
st_time = drivingprofiletest.st_time;
end_time = drivingprofiletest.end_time;
occupation = drivingprofiletest.occupation;
housing = drivingprofiletest.housing;
carsize = drivingprofiletest.car_size;
state = drivingprofiletest.state;
regiostar = drivingprofiletest.regiostarGem7;
% select value(s) to be analyzed
value = drivingprofiletest.realdistance;
%DURATION OF PARKING AND DRIVING
t_End = datetime(drivingprofiletest.end_time,'InputFormat','HH:mm');
t_St = datetime(drivingprofiletest.st_time,'InputFormat','HH:mm');
tt_End = hour(t_End) + minute(t_End)/60;
tt_St = hour(t_St) + minute(t_St)/60;
drivingprofiletest.duration = tt_End - tt_St;
duration = drivingprofiletest.duration;
timediff = duration.*60;
singularvalue = value./timediff;
Tzeros_LSH = zeros(24*60+1,1);
traffic = zeros(24*60+1,1);
%% INTERVAL SPREAD
for i = 1:length(end_time)
[Y, M, D, H, MN, S] = datevec(st_time(i));
TrSt = H*60+MN+1;
[Y, M, D, H, MN, S] = datevec(end_time(i));
TrEn = H*60+MN+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
%% TIME AXIS
close all
t_min = 1:length(Tzeros_LSH);
t_hour = t_min/60;
%T2 = Tzeros./max(Tzeros);
%VelMat2 = VelMat./max(VelMat);
%% PLOTTING
figure
plot(t_hour,traffic)
xlim([1 24])
xlabel("Time (h)")
ylabel("Load shifting potential (kW)")
%ylabel("Load shifting potential (kW)") % label variable
grid on
I don't know if my brain stopped working now, but is there any way to generate graph in the same plot, but for each occupational category and without extracting each unique category, because later I wan to do the same with States, car_size etc?. For example: for full-time is one line, for pensioners is another etc. Thanks for the help beforehand. (data used is attached)
댓글 수: 0
채택된 답변
Cris LaPierre
2020년 8월 13일
편집: Cris LaPierre
2020년 8월 13일
There is likely a better way to do this, but here is how I could do it. I've modified your code slightly to take advantage of MATLAB's time handling capabilities (duration). I've also moved a large part of the code to a function so that it is easier to call for each grouping.
I'm not really using the splitapply function the way it was meant to be used, so apologies if this gets confusing. I found this post helpful.
load('driving_profiletest.mat');
% convert start, end times to durations (in minutes)
drivingprofiletest.st_time = minutes(duration(drivingprofiletest.st_time,'InputFormat','hh:mm'));
drivingprofiletest.end_time = minutes(duration(drivingprofiletest.end_time,'InputFormat','hh:mm'));
drivingprofiletest.end_time(drivingprofiletest.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(drivingprofiletest(:,["realdistance","end_time","st_time"]));
plotTraffic(D);
% format plot
xlim([1 24])
xlabel("Time (h)")
ylabel("Load shifting potential (kW)")
grid on
% Identify groups and corresponding names
[G,ID] = findgroups(drivingprofiletest.state);
Y = splitapply(@plotTraffic,D,G);
legend(["All";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;
timediff = duration;
singularvalue = value./timediff;
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)
hold off
% I get an error message if I don't return something.
out = true;
end
Now if you want to plot by some other category, you would only need to change the variable in the findgroups code. For example, here is how I would modify it to plot by state.
[G,ID] = findgroups(drivingprofiletest.state);
댓글 수: 4
Cris LaPierre
2020년 8월 13일
Remove the following line of code.
plotTraffic(D);
You will need to do some repositioning of the code that formats the plot and modify the line that creates the legend as well.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Sensor Fusion and Tracking Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!