MATLAB: Convert x-axis values to datetime

axes propertiesdatetimedatetimerulernumericruler

How can I convert numeric x-axis values to datetime values? I have a plot with time in seconds (from a specific epoch) and I'd like to convert the values to datetime values. I know that plot can take datetime in the initial input, but if that is not an option, can I convert the axis from a NumericRuler to a DatetimeRuler after the plot has been generated?

Best Answer

If you have an existing plot with datetime axes, then you can set the XRuler of the second plot to be the XRuler of the first plot:

ax2.XRuler = ax1.XRuler

In my test in R2018a, the X axis display disappears from the old location and appeared in the new location, but with changed limits and changed tick locations reflecting the different span of XData that I had for the two. Changing the properties in the old axis changed the properties in the new axis as well, but the display updates were still only for the new location. Thus the XRuler object itself has its handle copied, but the display system is only prepared for an XRuler to be displayed in one place at a time (only one Parent, I guess.)

Related Question