How to graph values of different lengths?

3 views (last 30 days)
Nicole
Nicole on 12 Oct 2023
Answered: Fabio Freschi on 12 Oct 2023
Hello, I am trying to plot two values against eachother and I keep getting the error that they are different lengths and I'm not sure how to fix it. I am trying to plot the amount "people are charging their vehicles" (charging_duration in the code) against the "time of day they are charging their vehicle" (ChargeStartHour in the code) and I have these values for 21 different peopple in the matrix. This is my current code:
clear all
close all
%Step 1: read the load profile and upsampling it to 1-minute data
housepower = readmatrix('Summer_Smart_Meter.csv');
% note that column 1 is time and cannot be read correctly
housepower_a = housepower(:,2:22); %phase a. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
housepower_b = housepower(:,23:43); %phase b. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
housepower_c = housepower(:,44:64); %phase c. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
time_15min = [0:15:15*4*24*7-1]'; %time every 15-minute
time_1min = [0:1:60*24*7-1]'; %time every 1-minute
for i = 1:21 % This is for house 1. To calculate houses 1 to 21, you need to modify this part of the code
house1_15min = housepower_c(:,i);
% interpolation 14 data points so it becomes 1-minute data
house1_1min = interp1(time_15min,house1_15min ,time_1min);
end
figure(1)
plot(time_15min/60/24,house1_15min,'o',time_1min/60/24,house1_1min,':.'); % divided by 60 then by 24 turns minute into day
xlabel('Time (day)')
ylabel('Power (kW)')
%Step 2: Generate the EV profile for house 1
ChargeStartHour = [ 23 20 16 0 0 16 0 15 18 0 0 14 0 0 15 17 20 18 19 16;
0 0 37 45 42 40 0 39 41 0 0 44 0 0 41 38 0 41 40 41;
66 67 55 69 0 64 0 63 66 0 0 67 67 0 62 68 0 66 0 66;
0 0 92 90 0 88 0 87 89 92 0 91 0 86 87 86 91 90 90 89;
106 115 118 115 117 111 104 112 115 117 116 117 0 0 110 0 116 113 0 113;
142 0 139 0 0 0 0 141 139 140 0 138 0 0 135 0 0 137 135 137;
164 0 157 161 156 163 0 161 162 0 164 161 163 157 158 149 164 161 157 161]; %hour
charging_duration = [ 5 36 163 0 0 398 0 54 222 0 0 62 0 0 60 70 99 42 374 386;
0 0 163 511 89 283 0 54 208 0 0 61 0 0 240 98 0 40 5 27;
96 77 163 237 0 283 0 54 222 0 0 78 93 0 15 126 0 42 0 30;
0 0 192 326 0 283 0 54 209 274 0 68 0 107 50 90 67 40 307 114;
72 127 115 256 124 283 370 125 72 330 240 61 0 0 26 0 97 48 0 61;
48 0 101 0 0 0 0 14 72 330 0 117 0 0 92 0 0 175 45 28;
355 0 58 554 138 206 0 144 72 0 106 53 93 201 56 336 108 89 269 12]; % in minutes
charging_power = [3 6 3 3 6 3 6 3 6 6 6 3 3 6 6 6 6 6 6 6];
EV_power = zeros(24*60*7,1); % initialization
time = (0:1:60*24*7-1)';
for j = 1:20 % This is for house 1. To calculate houses 1 to 21, you need to modify this part of the code
for i = 1:7 %assuming 15-minute data resolution. Note the EV charging can start at any minutes within the hour
time_start = ChargeStartHour(i,j)*60 + floor(rand()*60); % Use floor to turn the number into integer value.
time_end = time_start + charging_duration(i,j);
EV_power(time_start:time_end-1,1) = charging_power(1, j) ;
end
end
figure(2)
plot(time/60/24, EV_power)
xlabel('Time (day)')
ylabel('Power (kW)')
The error I am recieving is this:
Error using plot
Vectors must be the same length.
Error in projectstep2_451 (line 56)
plot(time/60/24, EV_power)
I can see that the vectors are different lengths in my matlab workspace, but based on my calculations in the for loop I'm not sure why they are. Any help would be appreciated, thank you!!

Answers (2)

Star Strider
Star Strider on 12 Oct 2023
The ‘time’ vector has 10080 elements and ‘EV_power’ has 10230 elements.
One way to fix that is to create:
time = linspace(0, 60*24*7-1, numel(EV_power));
and while that works to produce the plot, the best resolution would be to go through the code to see what the problem is in constructing the two vectors.
% %Step 1: read the load profile and upsampling it to 1-minute data
% % housepower = readmatrix('Summer_Smart_Meter.csv');
% % note that column 1 is time and cannot be read correctly
% housepower_a = housepower(:,2:22); %phase a. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
% housepower_b = housepower(:,23:43); %phase b. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
% housepower_c = housepower(:,44:64); %phase c. 21 houses and 4 data point per hour for 7 days. 4*24*7=672 points.
% time_15min = [0:15:15*4*24*7-1]'; %time every 15-minute
% time_1min = [0:1:60*24*7-1]'; %time every 1-minute
% for i = 1:21 % This is for house 1. To calculate houses 1 to 21, you need to modify this part of the code
% house1_15min = housepower_c(:,i);
% % interpolation 14 data points so it becomes 1-minute data
% house1_1min = interp1(time_15min,house1_15min ,time_1min);
% end
%
% figure(1)
% plot(time_15min/60/24,house1_15min,'o',time_1min/60/24,house1_1min,':.'); % divided by 60 then by 24 turns minute into day
% xlabel('Time (day)')
% ylabel('Power (kW)')
%Step 2: Generate the EV profile for house 1
ChargeStartHour = [ 23 20 16 0 0 16 0 15 18 0 0 14 0 0 15 17 20 18 19 16;
0 0 37 45 42 40 0 39 41 0 0 44 0 0 41 38 0 41 40 41;
66 67 55 69 0 64 0 63 66 0 0 67 67 0 62 68 0 66 0 66;
0 0 92 90 0 88 0 87 89 92 0 91 0 86 87 86 91 90 90 89;
106 115 118 115 117 111 104 112 115 117 116 117 0 0 110 0 116 113 0 113;
142 0 139 0 0 0 0 141 139 140 0 138 0 0 135 0 0 137 135 137;
164 0 157 161 156 163 0 161 162 0 164 161 163 157 158 149 164 161 157 161]; %hour
charging_duration = [ 5 36 163 0 0 398 0 54 222 0 0 62 0 0 60 70 99 42 374 386;
0 0 163 511 89 283 0 54 208 0 0 61 0 0 240 98 0 40 5 27;
96 77 163 237 0 283 0 54 222 0 0 78 93 0 15 126 0 42 0 30;
0 0 192 326 0 283 0 54 209 274 0 68 0 107 50 90 67 40 307 114;
72 127 115 256 124 283 370 125 72 330 240 61 0 0 26 0 97 48 0 61;
48 0 101 0 0 0 0 14 72 330 0 117 0 0 92 0 0 175 45 28;
355 0 58 554 138 206 0 144 72 0 106 53 93 201 56 336 108 89 269 12]; % in minutes
charging_power = [3 6 3 3 6 3 6 3 6 6 6 3 3 6 6 6 6 6 6 6];
EV_power = zeros(24*60*7,1); % initialization
time = (0:1:60*24*7-1)';
for j = 1:20 % This is for house 1. To calculate houses 1 to 21, you need to modify this part of the code
for i = 1:7 %assuming 15-minute data resolution. Note the EV charging can start at any minutes within the hour
time_start = ChargeStartHour(i,j)*60 + floor(rand()*60); % Use floor to turn the number into integer value.
time_end = time_start + charging_duration(i,j);
EV_power(time_start:time_end-1,1) = charging_power(1, j) ;
end
end
SizeTime = size(time)
SizeTime = 1×2
10080 1
SizeEV_power = size(EV_power)
SizeEV_power = 1×2
10230 1
time = linspace(0, 60*24*7-1, numel(EV_power))
time = 1×10230
0 0.9853 1.9707 2.9560 3.9413 4.9267 5.9120 6.8974 7.8827 8.8680 9.8534 10.8387 11.8240 12.8094 13.7947 14.7800 15.7654 16.7507 17.7360 18.7214 19.7067 20.6921 21.6774 22.6627 23.6481 24.6334 25.6187 26.6041 27.5894 28.5747
figure(2)
plot(time/60/24, EV_power)
xlabel('Time (day)')
ylabel('Power (kW)')
.

Fabio Freschi
Fabio Freschi on 12 Oct 2023
@Nicole, you initialize the vector EV_power to have length 24*60*7 = 10080, however in the two nested loops, the dimesion changes, according to the values of time_start and time_end

Community Treasure Hunt

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

Start Hunting!