How to plot two data set with two different durations in the same plot?

3 vues (au cours des 30 derniers jours)
I have two datasets of electric load created by EVs, but each has different ending times, but the values remain the same. So second one, when I plot should be more flat. How do I plot two datasets with different durations in the same plot? Example data is attached.
StackOverflowEx = convertvars(StackOverflowEx, {'st_time', 'end_time'}, 'string');
st_time = StackOverflowEx.st_time;
end_time = StackOverflowEx.end_time;
value = StackOverflowEx.PevrReal; % calculated value from excel to distribute
% evenly over the interval time
timediff = StackOverflowEx.tchmin; % time of end and start time difference in minutes
singularvalue = value./timediff;
singularvalue(~isfinite(singularvalue)) = 0;% value per hour
Tzeros_LSH = zeros(24*60+1,1); % from 1 to 1440 scale is created to simulate day
% in minutes (+1 is for to avoid 0 values in
% time)
traffic_DCH = zeros(24*60+1,1); % scale for creating traffic (Discharging potential or Load shifting in this case)
%% 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_DCH(TrSt:TrEn,1) = Tzeros_LSH(TrSt:TrEn,1) + 1;
traffic_DCH(TrSt:TrEn,1) = traffic_DCH(TrSt:TrEn,1) + singularvalue(i); % adding
% singular values
end
end
%% TIME AXIS
close all
TimeM = 1:length(Tzeros_LSH);
TimeH = TimeM/60;
%% PLOT(TimeH,T2 ,TimeH)
figure
plot(TimeH,traffic_DCH)
xlim([1 24])
xlabel("Time (h)")
ylabel("Load (kW)")
grid on
Since the values are the same, I have added second ending time as "end_time2", and this code I was using for plotting only the first one. I couldn't figure out how to add second one to plot in the same graph. Also, my data is almost one million entries and it's taking too long time to execute. Maybe there is more efficient ways to do so? Thanks beforehand.

Réponse acceptée

Sourabh Kondapaka
Sourabh Kondapaka le 18 Sep 2020
Modifié(e) : Sourabh Kondapaka le 18 Sep 2020
Hi,
In the "StackOverflowEx.xlsx" file, there are 5 columns:
  • hp_id
  • st_time
  • end_time1
  • end_time2
  • PevReal
Hence your line 1, line 3, line 4 and line 7 are referring to columns which do not exist in the file(and will throw an erorr) as shown below:
%Only end_time1 and end_time2 are present in the dataset.
StackOverflowEx = convertvars(StackOverflowEx, {'st_time', 'end_time'}, 'string');
% same goes here
end_time = StackOverflowEx.end_time;
%Shouldn't it be StackOverflowEx.PevReal instead of StackOverflowEx.PevrReal
value = StackOverflowEx.PevrReal;
% The column tchmin doesn't exist in the dataset
timediff = StackOverflowEx.tchmin;
After resolving the above issues, you can plot end_time1 and end_time2 in the same figure by using the "hold on" keyword.
As you mentioned that the dataset contains close to a million rows and is taking too much time, I believe the for loop is where a lot of the time is being consumed:
%% 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_DCH(TrSt:TrEn,1) = Tzeros_LSH(TrSt:TrEn,1) + 1;
traffic_DCH(TrSt:TrEn,1) = traffic_DCH(TrSt:TrEn,1) + singularvalue(i); % adding
% singular values
end
end
You can vectorize as following:
%% INTERVAL SPREAD
[Y, M, D, H, MN, S] = datevec(st_time);
TrSt = H*60+MN+1;
[Y, M, D, H, MN, S] = datevec(end_time1); %% You can do the same for end_time2
TrEn = H*60+MN+1;
%% removing entries of NaN values in TrEn and TrSt as follows
TrSt = TrSt(~isnan(TrSt))
TrEn = TrEn(~isnan(TrEn))
Tzeros_DCH(TrSt:TrEn,1) = Tzeros_LSH(TrSt:TrEn,1) + 1;
traffic_DCH(TrSt:TrEn,1) = traffic_DCH(TrSt:TrEn,1) + singularvalue; % adding singular values
I would recommend you to do the "Matlab On Ramp Course" which can be found here. It teaches the basics of vectorizing operations.
Now when plotting you can use the "hold on" keyword.
For more information on "hold on", please refer the following link : here
The following documentation could also help : Combine Multiple Plots
A similar question has been answered in the community : here and here
  3 commentaires
Sourabh Kondapaka
Sourabh Kondapaka le 18 Sep 2020
Hi,
MATLAB is optimized for operations involving matrices and vectors. A lot of inbuilt functions provided by MATLAB are optimized for vectorization. For example in the above code "datevec()" function can be applied to a scalar or a vector.
If the function "datevec()" is applied to a vector, Instead of sequentially applying this function to each value in the vector, MATLAB will automatically start applying this function to more than 1 value of the vector based on the number of cores available on your CPU.
Thus, higher the number of cores on your CPU faster the execution of vectorized code.
This significantly improves the computation speed.
You can read up more on Vectorization here
Asrorkhuja Ortikov
Asrorkhuja Ortikov le 19 Sep 2020
Thanks mat, good to learn.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Plasma Physics dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by