How to plot the difference between y components for all x?

1 view (last 30 days)
I have plotted the energy consumption of two model house on one plot using roughly the following code (I do not have the exact code in-front of me):
t = 0:24; %hour of day
iEc = [measured values for model A]; %length =25
Ec = [measured values for model B;] %length = 25
figure
plot(t, iEc, '-or', t, Ec, '-ob'); %after this, there are graph labels
I am satisfied with how everything turned out but just as an extra feature, I'd like to add a line between the y component of the two curves at the same t value on the same graph. For example, at t = 1 , i'd like to plot a line from (y of iEc) to (y of Ec) and list the difference next to it. This is similar to what I'd like to do but I'd like to add the difference between the curves: i.stack.imgur.com/G2Azd.png
I'm not 100% on how to do this, but I was thinking a for loop may be the best way to do this for all t. Any help or suggestions are appreciated. Thank you.

Accepted Answer

Joel Miller
Joel Miller on 12 Dec 2017
Hi, I think this does what you are looking for:
plot(t, iEc, '-or', t, Ec, '-ob', [t; t], [iEc; Ec], '-k')
str = num2cell(abs(iEc-Ec));
Xoffset = t+.1; %X offset from vertical lines
Yoffset = min([iEc; Ec])+abs(iEc-Ec)/2; %midpoint of vertical lines
text(Xoffset, Yoffset, str)
  2 Comments
SK
SK on 12 Dec 2017
Thank you that was exactly what I was looking for. However I'd also like to add the following cases:
(1) I'd like to change the offset for the range (10<t<15) when the values where the curve difference is ~0.1? I can change X offset but it changes for all (which is ok) but I am unable to offset the y component properly. Please see picture.
(2) How can I change it if I wanted the lines for a specific set of numbers (ex:t = 3, 8, 18, 23)?
SK
SK on 12 Dec 2017
Edited: SK on 12 Dec 2017
I have tied it this way:
plot(t, iEc, '-ro', t, Ec, '-bo',[t; t], [iEc; Ec], '-k ');
hold on
for t = 0:24
if t>=10 && t<=15
str = num2cell(abs(iEc-Ec ));
Xoffset = t + .25;
Yoffset = max([iEc; Ec]) + 1;
text(Xoffset, Yoffset, str)
else
str = num2cell(abs(iEc-Ec));
Xoffset = t + .1;
Yoffset = min([iEc; Ec]) + abs(iEc-Ec)/2;
text(Xoffset, Yoffset, str)
end
end
but it give me the following errors:
Error using text: X and Y must be the same length Error in sgcs_final (line 27): text(Xoffset, Yoffset, str)
any suggestion on what to do from here?

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!