How to Put datetime into numeric axis

11 ビュー (過去 30 日間)
Heyuan Huang
Heyuan Huang 2018 年 7 月 12 日
コメント済み: Walter Roberson 2021 年 3 月 2 日
Hi all,
So I recently wrote a script that can dynamically change x and y axes' data. However, I cannot find a way to plot Date on x-axis.
I use the following way to change the data on axes: (suppose p is the plot line object handle, and somedata is the data I want to show on the axis)
p.XData=somedata;
Everything worked fine if somedata is switching between different double vectors. But if I want to plot date information on x axis, ie. somedata is a datetime vector, then I will get an error saying that the XData must be numeric vector.
I identified the problem to be that the XAxis property of p.Parent (an axes object) is a NumericRuler object, while in order to plot datetime information, it needs to be DatetimeRuler. However, I cannot find a way to change that XAxis property. I cannot find a command that enables me to create DatetimeRuler to replace the NumericRuler.
Can somebody help me with this issue? Thank you very much!
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 7 月 12 日
It is possible to take an existing DatetimeRuler and use it to replace the XRuler of an existing axes, such as
firstax.XRuler = secondax.XRuler
but this leaves kind of a strange state, and deleting the source axes secondax can cause the firstax.XRuler to become empty.
Heyuan Huang
Heyuan Huang 2018 年 7 月 12 日
Yep, I have tried that. Creating an empty and invisible figure. Then plot a datetime point, extract the datetimeruler and replace the one in real plot. Since I don't want to accumulate many figure objects, I delete each one after copying the ruler object. But then, as you said, the one I copied also gets deleted. So I was going to use the copyobj, but I am wondering if there is any other easy way around this.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 7 月 12 日
  2 件のコメント
Ted Shultz
Ted Shultz 2021 年 3 月 2 日
from Walter's link, here is what i came up wtih:
ruler2num is the function you need. it convets from the axis ruler to the axis number. Basicaly you setup a plot wtih datetime on the x axis, and then you can use imagesc to plot using the numeric values that corispond to your datetimes.
y=[3 7]; % just random number of y axis example
x=[datetime, datetime+days(5)]; % will plot image from now to five days from now
A = imread('ngc6543a.jpg'); % this is the image we will use
h1= plot(x,zeros(size(x))); % make a random plot with time as the x axis (will delete this in a second)
hold on
ax1 = gca; % get the current axis
h2= imagesc(ruler2num(x, ax1.XAxis),y,A); % use ruler2num() to plot this at the location we want
delete(h1); % delete this line we made before
Walter Roberson
Walter Roberson 2021 年 3 月 2 日
Thanks, Ted.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxes Appearance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by