MATLAB: How to Display Grid Lines with Specific Distance from One Another

grid linesplot

I want grid lines for a plot but MATLAB automatically define the number of grid lines. Is there any way to manually define the number of grid lines for a plot?

Best Answer

MATLAB put grid lines at every XTick or YTick position. You can set the axes XTick and YTick properties to change where the grid lines are to go.
For example:
number_interior_x_gridlines = 7;
ax = gca;
XL = get(ax, 'XLim');
xticks_at = linspace(XL(1), XL, number_interior_x_gridlines + 2);
set(ax, 'XTick', xticks_at);