plotyy error function,Vectors must be the same lengths.

조회 수: 5 (최근 30일)
haithem awijen
haithem awijen 2018년 11월 2일
댓글: haithem awijen 2018년 11월 5일
Hi i want to plot a graph with two y-axes, the code shows me an error saying that : Error using plot Vectors must be the same lengths.
Error in C:\Program Files\MATLAB\R2012b\toolbox\matlab\graph2d\plotyy.p>fevalfun (line 342)
Error in C:\Program Files\MATLAB\R2012b\toolbox\matlab\graph2d\plotyy.p>plotyy (line 83)
Error in plotregimes (line 21)
[AX,H1,H2]=plotyy(t,z1,t,data,'bar','plot');
here is my code :
clear
clc
close all
delete *.asv
%addpath('C:\Users\ISSG\Desktop\PublishedCode\ToolKit')
load 'C:\Users\user\Desktop\PublishedCode\4regimes\st.txt'
z=st(:,4:end);
load 'C:\Users\user\Desktop\PublishedCode\4regimes\datain6.txt'
data=datain6(7:end,:);
z1=prctile(z==1,50);
z2=prctile(z==2,50);
z3=prctile(z==3,50);
z4=prctile(z==4,50);
t=1965+(7/12):1/12:2011+(3/12);
figure(1)
orient landscape
subplot(2,2,1);
[AX,H1,H2]=plotyy(t,z1,t,data,'bar','plot');
title('Probability of Regime 1');
set(AX(1),'xlim',[min(t) max(t)]);
set(AX(2),'xlim',[min(t) max(t)]);
set(AX(1),'ylim',[0 1]);
set(AX(2),'ylim',[min(min(data)) max(max(data))]);
set(AX(1),'Ycolor','b');
set(AX(2),'Ycolor','k');
subplot(2,2,2);
[AX,H1,H2]=plotyy(t,z2,t,data,'bar','plot');
title('Probability of Regime 2');
set(AX(1),'xlim',[min(t) max(t)]);
set(AX(2),'xlim',[min(t) max(t)]);
set(AX(1),'ylim',[0 1]);
set(AX(2),'ylim',[min(min(data)) max(max(data))]);
set(AX(1),'Ycolor','b');
set(AX(2),'Ycolor','k');
subplot(2,2,3);
[AX,H1,H2]=plotyy(t,z3,t,data,'bar','plot');
title('Probability of Regime 3');
set(AX(1),'xlim',[min(t) max(t)]);
set(AX(2),'xlim',[min(t) max(t)]);
set(AX(1),'ylim',[0 1]);
set(AX(2),'ylim',[min(min(data)) max(max(data))]);
set(AX(1),'Ycolor','b');
set(AX(2),'Ycolor','k');
subplot(2,2,4);
[AX,H1,H2]=plotyy(t,z4,t,data,'bar','plot');
title('Probability of Regime 4');
set(AX(1),'xlim',[min(t) max(t)]);
set(AX(2),'xlim',[min(t) max(t)]);
set(AX(1),'ylim',[0 1]);
set(AX(2),'ylim',[min(min(data)) max(max(data))]);
set(AX(1),'Ycolor','b');
set(AX(2),'Ycolor','k');
legend('Probability','Federal Funds Rate ','Bond Yield Spread','Unemployment rate','CPI Inflation','M2 growth','Stock Price growth');
print ../figure_files/RegimesFig -dpdf
  댓글 수: 3
haithem awijen
haithem awijen 2018년 11월 5일
편집: haithem awijen 2018년 11월 5일
here is the figure i would want to have
Walter Roberson
Walter Roberson 2018년 11월 5일
I would not recommend bar plot for this purpose. I would recommend using fill() to shade an area.

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

답변 (3개)

madhan ravi
madhan ravi 2018년 11월 2일
[AX,H1,H2]=plotyy(t(1:numel(z1)),z1,t(1:numel(data)),data,'bar','plot');
  댓글 수: 4
haithem awijen
haithem awijen 2018년 11월 5일
thank you so much, Here is the code , txt files and the figure deisred
haithem awijen
haithem awijen 2018년 11월 5일
i tried your sugesstions but it plot this figure:
clear
clc
close all
delete *.asv
load 'C:\Users\ISSG\Desktop\PublishedCode\4regimes\st.txt'
z=st(:,4:end);
load 'C:\Users\ISSG\Desktop\PublishedCode\4regimes\datain6.txt'
data=datain6(7:end,:);
z1=prctile(z==1,50);
z2=prctile(z==2,50);
z3=prctile(z==3,50);
z4=prctile(z==4,50);
t=1965+(7/12):1/12:2011+(3/12);
figure(1)
orient landscape
subplot(2,2,1);
title('Probability of Regime 1');
yyaxis left
plot(t,z1,'b');
yyaxis right
bar(t,z1)
subplot(2,2,2);
title('Probability of Regime 2');
yyaxis left
plot(t,z2,'b');
yyaxis right
bar(t,z2)
subplot(2,2,3);
title('Probability of Regime 3');
yyaxis left
plot(t,z3,'b');
yyaxis right
bar(t,z3)
subplot(2,2,4);
title('Probability of Regime 4')
yyaxis left
plot(t,z4,'b');
yyaxis right
bar(t,z4)
legend('Probability','Federal Funds Rate ','Bond Yield Spread','Unemployment rate','CPI Inflation','M2 growth','Stock Price growth');
end

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


haithem awijen
haithem awijen 2018년 11월 5일
it shows another error :
Index exceeds matrix dimensions.

haithem awijen
haithem awijen 2018년 11월 5일
here the code and txt file

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by