Plotting within a FOR loop

3 views (last 30 days)
Gautam
Gautam on 4 Apr 2014
Commented: Joseph Cheng on 4 Apr 2014
Hello, some help please...
I have three column vectors T_e, T_c and P (each of size 56x1). I need to plot T_e vs P (at constant T_c). T_e is a vector in which the first 8 elements get repeated 7 times. P has the values corresponding to each T_e at the corresponding values of T_c. I have displayed the column vectors below:
T_e=[10 15 20 25 30 35 40 45 10 15 20 25 30
35 40 45 10 15 20 25 30 35 40 45 10 15
20 25 30 35 40 45 10 15 20 25 30 35 40
45 10 15 20 25 30 35 40 45 10 15 20 25
30 35 40 45]'
T_c=[ 140 140 140 140 140 140 140 140 130 130 130 130 130
130 130 130 120 120 120 120 120 120 120 120 110 110
110 110 110 110 110 110 100 100 100 100 100 100 100
100 90 90 90 90 90 90 90 90 70 70 70 70
70 70 70 70]'
P=[6400 6950 7450 7950 8350 8750
9100 9400 6250 6750 7200 7600
7950 8250 8500 8750 6100 6500
6850 7200 7450 7700 7900 8050
5850 6200 6450 6700 6900 7100
7200 7300 5550 5800 6050 6200
6350 6450 6500 6500 5200 5400
5550 5650 5700 5750 5750 5700
4400 4460 4470 4440 4380 4280
4150 3990]'
Please note that despite how it is being displayed here, all three are column vectors - I copied them from the workspace.
I need a generalized code that will be able to plot T_e vs P for this data set and many other data sets too.
The program I wrote for this is:
for i <= 1:size(T_e)
if T_e(i) <= T_e(i+1)
i = i+1;
else
disp(i)
plot(T_e(1:i),Powerinput(1:i))
i = i+1;
end
end
It seems to work reasonably well. The trouble is it generates 6 different plots - I want one. It connects the end point of the first curve to the start point of the second curve with a straight line. I want the curves to be independent lines and of different colors. How do I do that?
And although the for loop is supposed to end at size(x), it keeps going one higher and ends with this warning:
Index exceeds matrix dimensions.
Error in proplot (line 15) if EvaporatingT(i) <= EvaporatingT(i+1)
What I ideally want is this:
plot(EvaporatingT(1:8),Powerinput(1:8),'b', EvaporatingT(9:16),Powerinput(9:16),'g', EvaporatingT(17:24),Powerinput(17:24),'m', EvaporatingT(25:32),Powerinput(25:32),'r', EvaporatingT(33:40), Powerinput(33:40),'c', EvaporatingT(41:48), Powerinput(41:48),'k', EvaporatingT(49:56), Powerinput(49:56),'y');
hold on
plot(EvaporatingT(1:8), Massflowrate(1:8),'b', EvaporatingT(9:16),Massflowrate(9:16),'g', EvaporatingT(17:24),Massflowrate(17:24),'m', EvaporatingT(25:32),Massflowrate(25:32),'r', EvaporatingT(33:40),Massflowrate(33:40),'c', EvaporatingT(41:48), Massflowrate(41:48),'k', EvaporatingT(49:56),Massflowrate(49:56),'y');
xlabel('EvaporatingT'); ylabel('Powerinput')
legend('CondensingT = 140','CondensingT = 130','CondensingT = 120','CondensingT = 110','CondensingT = 100','CondensingT = 90','CondensingT = 70','Location','Northwest')
but in a program so that I can apply it to many different data sets.
Any assistance would be greatly appreciated. Thanks very much in advance.

Answers (1)

Joseph Cheng
Joseph Cheng on 4 Apr 2014
a for loop to do the first part can be then used for the rest of your plots
plot(EvaporatingT(1:8),Powerinput(1:8),'b', EvaporatingT(9:16),Powerinput(9:16),'g', EvaporatingT(17:24),Powerinput(17:24),'m', EvaporatingT(25:32),Powerinput(25:32),'r', EvaporatingT(33:40), Powerinput(33:40),'c', EvaporatingT(41:48), Powerinput(41:48),'k', EvaporatingT(49:56), Powerinput(49:56),'y');
can be simplified to something like this
for i =1:96/8
x(:,i) = [1:8]';
end
x=x(:);
y=randi(100,1,96);
cc=hsv(length(x));
figure,hold on,cc=hsv(length(x));
for i =1:8:length(x)
plot(x(i:i+7),y(i:i+7),'color',cc(i,:))
end
  2 Comments
Gautam
Gautam on 4 Apr 2014
Edited: Gautam on 4 Apr 2014
Thanks very much for the reply. In your code you have assumed that the data repeats after every 8 elements - which is correct for this data set, but wouldn't apply to all other data sets - that is the reason I need a program to identify when the one set of data points ends and the other begins, eg.: the program I posted above returns index = 8 and plots T_e and Powerinput from T_e = 10:45 as one curve. The second set of 10:45 will need to be plotted as a second curve. Please copy the data and use the program to get a complete idea of what I mean. Thank you once again.
Joseph Cheng
Joseph Cheng on 4 Apr 2014
well i can't really do that because you didn't supply powerinput and other things besides T_E T_c and P. The sample i gave with dummy numbers can be adapted to fit your needs

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!