Search code examples
matlabplotaxis-labels

How to use ticks in axes without specifying last value of tick?


I've a problem with the axis settings in MATLAB. I want to have more ticks in my x and y axis than default in MATLAB. For that I used the following function

ax = gca;
ax.XTick = 0:0.5:max(xlim); 
ax.YTick = 0:0.5:max(ylim);

The Problem is my y axis starts not at 0 but at -0.5. I know I can define the XLim and YLim, but if I try the following code:

ax.XLim = [0 inf]
ax.YLim = [0 inf]
ax = gca;
ax.XTick = 0:0.5:max(xlim); 
ax.YTick = 0:0.5:max(ylim);

I get the following error:

Maximum variable size allowed by the program is exceeded.
Error in XXX
ax.YTick = 0:0.5:max(ylim);

I use inf because I don't know the last value. All I want is a plot that starts at 0 for both axes and ticks every 0.5.


Solution

  • If I understand correctly, you simply wish to set the axis limits to start from 0, but don't want to change the upper limit.
    You used the following set the axis ticks:

    ax.XTick = 0:0.5:max(xlim); 
    

    Then you can use similar statement to set the limits:

    ax.XLim = [0 max(xlim)];