MATLAB: Plot with two different x-axix

2 x axixplotsecondaryx axis

Hello,
I have to plot two different values over a certain depth. Those two values have very different ranges, so I would like one of them (k1) to have its x axis at the top, and the other(NESPN1) its x axix on the bottom. I have tried the mathworks page on double x axis, but it won't work. Can you please help me?
if true
figure
plot(k1,Depth1)
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none');
plot(NESNP1,Depth1,'Parent',ax2,'Color','k')
set(gca,'Ydir','reverse')
end

Best Answer

Try this.
clear all;close all
figure;
h=plot(k1,Depth1)
ax1=gca
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none');
plot(NESNP1,Depth1,'Parent',ax2,'Color','k')
set(gca,'Ydir','reverse')
set(ax2,'XAxisLocation','top')
set(ax1,'Ytick',[])