How to avoid converting a great number of datenum values into datetime for tick labels?

3 次查看(过去 30 天)
Hi, I have some data sampled with 25 Hz for about 3 days so a lot of data points.
I have plotted the data in a figure with time values on x-axis that scales automatically when zooming.
The y-data are numeric values and the x-data are numeric datenum values and takes no time to plot.
I would like to have date values on the x-axis, but converting datenum values to datetime takes a lot of time.
Even if I zoom in a lot, I don´t need datetime tick values for all data points, one for each second or even one per minute could do.
That would decrease the number of datetime values needed to be converted for the ticklabes on the x-axis.
Is it possible to just use the datenum values for plotting and then use a lower number of datetime values for the tick labels and still keep the x-axis scalable?
If yes, then how to do it? ;)

采纳的回答

Anders Skovlund Hansen
Hi Steven,
Sorry if I haven´t been clear enough of what I am trying to accomplish...
It´s no problem for me to create a less dense datetime vector.
My problem is how to create a ticklabel axis with datetime values that scales automatically.
And the only reason for this is because my plot is slow at updating if I choose new data to plot because it has to convert all of the datenum values to datetime values instead of just some of them.
But thanks to your first question, regarding how I was doing the conversion, I figured out that I was converting the datenum values to a string before the final conversion to datetime values:
XData = datetime(datestr(XData))
I think I copied the line of code from somewhere and thought it might needed to be converted to datestr first, but I just figured out that I can apply this instead:
XData = datetime(XData,'ConvertFrom','datenum')
Now it converts more than 2 million datenum values to datetime values in 0.038462 second so that’s more than fine for me!
Thanks again 😊

更多回答(1 个)

Steven Lord
Steven Lord 2020-8-6
I would like to have date values on the x-axis, but converting datenum values to datetime takes a lot of time.
How are you performing this conversion? Are you using a for loop to convert them one at a time or are you passing an array into one call to datetime with the 'ConvertFrom' option?
Even if I zoom in a lot, I don´t need datetime tick values for all data points, one for each second or even one per minute could do.
Something like this?
N = datetime('now');
x = N:minutes(1):(N+hours(1))

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by