How to assign XTickLabels a subset of date strings?

4 views (last 30 days)
I'm using imagesc to plot ocean current magnitude against depth (y-axis) and time (x-axis). The call looks like this:
imagesc([1 1772], [depth(1) depth(end)], speed);
where depth is a vector of 38 non-regularly-spaced doubles (representing depth values), [1 1772] refers to the number of columns of the speed matrix (each column corresponds to a datetime object), and speed is a matrix whose elements contain ocean current magnitudes (38 rows for 38 depth values, 1772 columns for 1772 datetime objects).
There are 1772 datetime values for the x-axis so there's no way their corresponding datetime string can fit on the imagesc plot. How do I set the XTickLabels for selected x values (i.e., I'd only like there to be a timestamp for column 1, 186, 217, ... (corresponding to datetime objects that are 6 months apart))?
I tried creating a 1x1772 cellstr vector with empty cell strings where dates don't correspond to the dates I wanted displayed but only one date tick gets displayed (the first date):
XTickIdx = 1:186:1772; % Index into datetime vector; 10 values
c = cell(1,1772);
c(1:186:1772) = cellstr(datestr(t(XTickIdx),'mm/dd/yyyy')); % 't' is the datetime array
axes.XTickLabel = c;

Answers (1)

Adam Danz
Adam Danz on 31 Jul 2018
Edited: Adam Danz on 31 Jul 2018
Instead of setting XTickLabel, just set XTicks. Assuming your datetime vector is named 'dateTimeVector',
set(gca, 'xtick', dateTimeVector(1:186:1772))

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!