MATLAB Answers

Subplot error from two matrices

1 view (last 30 days)
James Wilson
James Wilson on 23 Apr 2021
Edited: DGM on 23 Apr 2021
I am trying to create a 4x2 subplot to show two functions on one subplot throughout the code, I copyed & pasted the code from the first line after I got it working, but I recieve the error: "Error in Generator (line 53) plot(x, usage(2:24), 'b', x, P_Energy(2:24), 'g');
I am pulling the information from plot from two 24x7 matrices.
Code:
P_Energy = Pan*Energy_WeatherAffect;
x = [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24];
figure
subplot(4,2,1);
plot(x, usage(1:24), 'b', x, P_Energy(1:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 1 Produced vs. Used')
subplot(4,2,2);
plot(x, usage(2:24), 'b', x, P_Energy(2:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 2 Produced vs. Used')
subplot(4,2,3);
plot(x, usage(3:24), 'b', x, P_Energy(3:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 3 Produced vs. Used')
subplot(4,2,4);
plot(x, usage(4:24), 'b', x, P_Energy(4:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 4 Produced vs. Used')
subplot(4,2,5);
plot(x, usage(5:24), 'b', x, P_Energy(5:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 5 Produced vs. Used')
subplot(4,2,6);
plot(x, usage(6:24), 'b', x, P_Energy(6:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 6 Produced vs. Used')
subplot(4,2,7);
plot(x, usage(7:24), 'b', x, P_Energy(7:24), 'g');
xlabel('Hour')
ylabel('Energy Produced')
grid on
title('Day 1 Produced vs. Used')
legend('green solid is Energy Produced', 'blue solid is Energy Used')
  1 Comment
DGM
DGM on 23 Apr 2021
You're plotting
usage(2:24) % 23 element vector
against
x % 24 element vector
I don't think that's what you want to do. You said your arrays are 24x7, and you're plotting daily usage, so maybe you want to be plotting usage(:,2) vs x and usage(:,3) vs x, and so on.
When you do usage(1:24), it does work, but only accidentally. In this syntax, you're doing a linear index into the array instead of using two subscripts. Linear indices operate columnwise, so you're implicitly selecting the first column. It's just a coincidence that that's correct for your needs in this one case.

Sign in to comment.

Accepted Answer

James Wilson
James Wilson on 23 Apr 2021
Nevermind, I released where I messed up the code my not doing (1:24,1) for each column I wanted to pull the information from causing the error.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by