MATLAB: “plot” and “stairs” in the same figure with different axis

axis positionplotstairs

Hello guys, for first excuse me for my bad English. I'm trying to plot in the same figure a graphic with "plot" function and a graphic with "stairs" function to represent the same signal, once analog and once quantized. So, I want to visualize in one figure:
  • the analog signal using plot function, X axis at the bottom and Y axis at the left;
  • the quantized signal using stairs function, X axis at the top and Y axis at the right;
I tried to specify axis's properties but everytime the output of plot function disappears; in addition to this, I can't display even just stairs function's output with X axis on the top and Y axis on the left.
ax1 = axes('XAxisLocation','bottom','YAxisLocation','left'
ax2 = axes('XAxisLocation','top','YAxisLocation','right');
plot(ax1,P,V_ina)
hold on
stairs(ax2,st,V_adc)
The last command doesn't work in every case. I tried also to specify parent's relationship for stairs function but the code still does not work. Obviously plot and stairs work well together usign "hold on" and the same axis. There is a guide that explains how to do a similiar thing but using only "line" function; can anyone help me? It is really strange that there is a yyaxis funtion and a similiar command for X axis doesn't exist.

Best Answer

x=rand(1,10);
y=1:10;
ax = gca; % current axes
ax1=plot(x,y)
hold on
ax2=stairs(x,y)
ax1=gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');