MATLAB Answers

Function for setting tick markers on a variable length x axis.


I'm using Matlab to create a (primitive) reverberation unit, where the time based output will obviously change each time the program is run dependent on various factors.

The output (in samples) will always be a multiple of Fs (44100). My current plot code is as follows:

    xlabel ('Time (Seconds)')
    set(gca,'XTick',[Fs/2 Fs 3*Fs/2 Fs*2 5*Fs/2 Fs*3])   %Set the points at which tick markers occur
    set(gca,'XTickLabel',{'0.5','1','1.5','2','2.5','3'})    %Sets the text under tick markers

This works for signals up to 3 seconds, but if I was to add anything longer then the tick markers will be absent (obvious). An amateur solution to this would simply be to create a very long list (say up to 10 seconds worth) of ticks that will appear when required (7*Fs/2 etc etc).

However, there must be a clever way of creating a function to do this (or there may be something already in Matlab I have overlooked?).

I was thinking along the lines of:

 T = (length(x)/Fs) %Gives time in seconds of output

Then based on that time having a function to create a vector of numbers that goes from T/(T*2), to T. Giving me tick markers from 0.5 to T seconds. Then a similar calculation for the X Labels.

(I'm amateur at function calling etc, so go easy on me!)

Thanks a lot,




No products are associated with this question.

1 Answer

Answer by John Barber
on 25 May 2013

Hi Chris,

I'm not exactly sure what you're looking for, but you might want to try my File Exchange program, xtickinterval, which you can find here: This will draw ticks and labels at a user-specified interval, regardless of the axis length. It will update the ticks/ticklabels automatically when the axis limits change.

If it doesn't do exactly what you want, you might get some ideas of how to implement it by looking through the code.



Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!