2
votes

I'm creating a 2d MATLAB plot. I'm setting the limits of my x axis, and letting my y axis auto-adjust (by setting its limits to [-inf inf]). After creating my plot, I need to check what my y axis has auto adjusted to (as I'm going to create a heatmap to put under my plot).

Unfortunately, ylim (and similar functions) only produce [-inf inf], not whatever the axes have adjusted to.

Some code which reproduces this problem (much more simply than my actual code) is:

function createplot(xbounds)
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
axis([xbounds,-inf,inf]);

createplot([0,10])

which produces a parabolic plot with y limits = [0,100]. However, ylim = [-inf, inf].

Any help would be appreciated!

/ Wilbur

2
@Shai is correct, the 3rd and 4th elements of axis will return [0 100] - bla
@Shai, for me (Matlab 2012a) it does work, I'll post it as an answer so you'll see the code I'm using. - bla

2 Answers

2
votes

As @Shai suggested, axis can give info regarding the ylimits without the need to set them to [-inf,inf] or use axis to set the x-axis bounds:

xbounds=[1 10]
x = xbounds(1):0.5:xbounds(2);
y = x.^2;
plot(x,y);
xlim([xbounds(1) xbounds(2)]);
v=axis 

v =
     1    10     0   100
2
votes

Looking at @natan's answer I think the solution to your problem is

Do not use [-inf inf] for auto-adjusting axis limits.

If you want Matlab to auto adjust some of your axes limits and manually set others, then you should use either xlim, ylim or zlim for the specific axis you wish to set and leave all the other unchanged so Matlab can set them automatically.
This way you will not override the values Matlab assigns to those axes and you will be able to read them using axis, xlim, ylim or zlim.

Please see @natan's answer for corrected code.