필터 지우기
필터 지우기

How to assign XTickLabels a subset of date strings?

조회 수: 1 (최근 30일)
Minh Tran
Minh Tran 2018년 7월 30일
편집: Adam Danz 2018년 7월 31일
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