Why will these plots not plot on the same graph?

조회 수: 5 (최근 30일)
Reid Johnson
Reid Johnson 2019년 9월 15일
편집: Walter Roberson 2022년 10월 25일
I think that matlab thinks the data has different axes for each plot, even though all the data has the same two axes. I get the data that im plotting from a matrix that I create earlier in the code. The columns are set up in a way that if column x is my x values, column x+1 is my y values, where x is an odd number. If I comment out the red graph, the green is plotted. If I comment out the green and red graph, the blue is plotted.
clc; clear;
load('rpm_kw.mat')
x=1;i=1;
rpmcol=1;
[m,n]=size(rpm_kw);
while rpmcol<=m-3
Astop=1;
conf=0;
while conf==0 %getting set A
if isnan(rpm_kw(Astop,rpmcol)) || isnan(rpm_kw(Astop,rpmcol+1)) %finding length of set A by running until nan
Astop=Astop-1; %location of the last value in set A
spdnpwr(1:Astop,x)=rpm_kw(1:Astop,rpmcol); %writing rpm values for set A
spdnpwr(1:Astop,x+1)=rpm_kw(1:Astop,rpmcol+1); %writing pwr values for set A
conf=1; %confirms set A values have been written to spdnpwr
else
Astop=Astop+1; %Increase Astop (done until NaN is found)
end
end
Bstop=1;
conf=0;
while conf==0 %getting set B
if isnan(rpm_kw(Bstop,rpmcol)) || isnan(rpm_kw(Bstop,rpmcol+1)) %finding length of set B by running until nan
Bstop=Bstop-1; %location of the last value in set B
spdnpwr(1:Bstop,x+2)=rpm_kw(1:Bstop,rpmcol); %writing rpm values for set B
spdnpwr(1:Bstop,x+3)=rpm_kw(1:Bstop,rpmcol+1); %writing pwr values for set B
conf=1; %confirms set B values have been written to spdnpwr
else
Bstop=Bstop+1; %Increase Bstop (done until NaN is found)
end
end
Cstop=Astop+Bstop;
y=1;a=1;b=1;
while y<=Cstop %getting set C
if spdnpwr(a,x)<spdnpwr(b,x+2) %if set A is less than set B, use set A for current set C value
spdnpwr(y,x+4)=spdnpwr(a,x); %rpm
spdnpwr(y,x+5)=spdnpwr(a,x+1); %pwr
a=a+1;
elseif spdnpwr(b,x+2)<spdnpwr(a,x) %if set B is less than set A, use set B for current set C value
spdnpwr(y,x+4)=spdnpwr(b,x+2); %rpm
spdnpwr(y,x+5)=spdnpwr(b,x+3); %pwr
b=b+1;
elseif spdnpwr(a,x)==spdnpwr(b,x+2) %if set B is equal to set A
if spdnpwr(a,x+1)<spdnpwr(b,x+3) %if set A pwr is less than set B, use set A for current set C value
spdnpwr(y,x+4)=spdnpwr(a,x); %rpm
spdnpwr(y,x+5)=spdnpwr(a,x+1); %pwr
a=a+1;
elseif spdnpwr(b,x+3)<spdnpwr(a,x+1) %if set B pwr is less than set A, use set B for current set C value
spdnpwr(y,x+4)=spdnpwr(b,x+2); %rpm
spdnpwr(y,x+5)=spdnpwr(b,x+3); %pwr
b=b+1;
elseif spdnpwr(a,x+1)==spdnpwr(b,x+3) %if set A pwr is equal to set B, use set A for current set C value and set B for next C value
spdnpwr(y,x+4)=spdnpwr(a,x); %rpm
spdnpwr(y,x+5)=spdnpwr(a,x+1); %pwr
spdnpwr(y+1,x+4)=spdnpwr(b,x+2); %rpm
spdnpwr(y+1,x+5)=spdnpwr(b,x+3); %pwr
a=a+1;b=b+1;y=y+1;
else
disp('You mucked up')
end
end
y=y+1;
end
%temp plots
subplot(4,4,i)
plot(spdnpwr(1:Astop,x),spdnpwr(1:Astop,x+1),'b')
plot(spdnpwr(1:Bstop,x+2),spdnpwr(1:Bstop,x+3),'g')
plot(spdnpwr(1:Cstop,x+4),spdnpwr(1:Cstop,x+5),'r')
xlabel('rpm')
ylabel('kw')
i=i+1;
rpmcol=rpmcol+4;
x=x+6;
end

답변 (1개)

Jim Riggs
Jim Riggs 2019년 9월 15일
편집: Jim Riggs 2019년 9월 15일
You need to turn on "hold" mode to plot more than one plot on the same axes. If you do not use hold, the next plot replaces the previous one.
plot(...'b');
hold on;
plot(... 'g');
plot(... 'r');
  댓글 수: 1
Reid Johnson
Reid Johnson 2019년 9월 15일
I've tried using hold before but it didn't work. I'll try again and put it in the location you have it in.
The only success I've had is using yyaxis, but that gives undesirable results.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by