How to assign XTickLabels a subset of date strings?

4 次查看(过去 30 天)
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;

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by