www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/ConvertNumericValueToEquivalentDateValueExample.m

    %% Convert Date Value to Equivalent Numeric Value
% Convert a date value to the equivalent numeric value in order to set the
% |Position| property of a text object.
%
% Create a plot with dates along the _x_-axis. Add
% a text description to the fourth data point and return the text object.
t = datetime(2015,1,1:10);
y = [.2 .3 .5 .2 .8 .2 .3 .1 .3 .4];
plot(t,y,'-o')
txt = text(t(4),y(4),'My text');

%%
% Change the position of the text to the sixth data point by setting the
% |Position| property of the text object. Since the |Position| property
% accepts only numeric values, convert the datetime value at the sixth data
% point to the equivalent numeric value.

ax = gca;
x6 = ruler2num(t(6),ax.XAxis);
txt.Position = [x6 y(6)];