MATLAB: Ylim is not working

ylim;

I have tried to inlcude a common and fixed Y-axis and X-axis for all the figures but it is working. Can you please help.
figure
N_F =1;
subplot(3,1,1);
idx_1 = MaT_All(:,1) == Median_All(1) & MaT_All(:,2) ~= Median_All(2) & MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6) & MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) ~= Median_All(8);
Mat_1 = MaT_All(idx_1,:);
scatter(MaT_All(idx_1,18),MaT_All(idx_1,17)); hold on;
title(sprintf('Impact of r* on Field %d',N_F))
xlabel('Risk ($B)')
ylabel('E(NPV) ($B)')
legend(sprintf('Medain Only of Field %d',N_F))
ylim([-50 50])
N_F =2;
subplot(3,1,2);
idx_2 = MaT_All(:,1) ~= Median_All(1) & MaT_All(:,2) == Median_All(2) & MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6) & MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) ~= Median_All(8);
Mat_2 = MaT_All(idx_2,:);
scatter(MaT_All(idx_2,18),MaT_All(idx_2,17)); hold on;
title(sprintf('Impact of r* on Field %d',N_F))
xlabel('Risk ($B)')
ylabel('E(NPV) ($B)')
legend(sprintf('Medain Only of Field %d',N_F))
ylim([-50 50])
N_F =8;
subplot(3,1,3);
idx_8 = MaT_All(:,1) ~= Median_All(1) & MaT_All(:,2) ~= Median_All(2) & MaT_All(:,3) ~= Median_All(3) & MaT_All(:,4) ~= Median_All(4)...
& MaT_All(:,5) ~= Median_All(5) & MaT_All(:,6) ~= Median_All(6) & MaT_All(:,7) ~= Median_All(7) & MaT_All(:,8) == Median_All(8);
Mat_8 = MaT_All(idx_8,:);
scatter(MaT_All(idx_8,18),MaT_All(idx_8,17)); hold on;
title(sprintf('Impact of r* on Field %d',N_F))
xlabel('Risk ($B)')
ylabel('E(NPV) ($B)')
legend(sprintf('Medain Only of Field %d',N_F))
ylim([-50 50])

Best Answer

Use explicit axes handles for plotting instructions and instructions like hold, ylim, xlim. i.e.
hAxes(1) = subplot(3,1,1);
scatter( hAxes(1),... );
ylim( hAxes(1),... );
...
hAxes(2) = subplot(3,1,2);
...
You may also find
doc linkaxes
useful if you want your axes to all have the same limits. If you store your axes handles in an array as shown above then you can simply replace all the individual ylim instructions with
linkaxes( hAxes, 'y' )
ylim( hAxes(1), [-50 50] )