# How to graph values of different lengths?

4 views (last 30 days)

Show older comments

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!!

##### 0 Comments

### Answers (2)

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)

SizeEV_power = size(EV_power)

time = linspace(0, 60*24*7-1, numel(EV_power))

figure(2)

plot(time/60/24, EV_power)

xlabel('Time (day)')

ylabel('Power (kW)')

.

##### 0 Comments

Fabio Freschi
on 12 Oct 2023

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!