Convert x-axis values to datetime
1 Comment
Accepted 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.)
2 Comments
More Answers (2)
1 Comment
There is no documented way to directly change the ruler type, but you can do it by plotting NaT (not a time), for example:
ax = axes; p = plot(NaT,NaN); delete(p);
Note that most plotting commands will reset the axes, so if you want to add things to the axes created in my example you will likely need to turn on hold. In addition, this will only work if the axes is empty. If hold is on, and you try to plot a different datatype, you will get an error about incompatible datatypes.
The specifics depend on what you are trying to do and the specific plotting commands. I believe any plotting commands which "understand" datetime (including plot, scatter, etc.) will error if you try plotting using a different datatype while hold is on. However, I believe other commands which are more low-level (and don't directly understand datetime, such as text and patch) will allow you to plot using numeric data into an axes configured for datetime data. In order for this to work, you need to use ruler2num to convert your data into doubles (in most situations, datenum will not do the conversion correctly).
You said it isn't an option to call plot with datetime inputs, why is that? What are you trying to plot? What is your overall goal?
2 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!