I want to convert the large datetime data into double format.

I used datenum command, but its changing the value. The code and the values are attached below. Here TimestampHide is the data from an excel sheet.
MATLAB Code:
>> T=TimestampHide;
>> T.Format = 'yyyy-MM-dd HH:mm:ss.SSS';
>> Z=datenum(T);
The value of Z

6 Comments

It appears that the resulting array Z has the same value for all its elements. Could you please provide a bit more detail about your question or the specific issue you're encountering?
"It appears that the resulting array Z has the same value for all its elements..."
No, simply that the time difference is less than the difference between two datenums at the fourth decimal place, fifth significant digit that is default command window display format.
Z=7.3884E5; % Z to the precision displayed
fprintf('%f\n',Z) % look at that more easily
738840.000000
datestr(Z) % see what date that really is...
ans = '15-Nov-2022'
A datenum is represented as the days since a reference time as the decimal portion of a double; the fractional part represents the fraction of a 24 hr day. So, the above datenum represents 00:00:00.000 on November 15, 2022.
Any time difference less than a day will show up as the same value with only five significant digits. Observe
Z(2)=Z+1; % add one
fprintf('%f\n',Z) % look at that more easily
738840.000000 738841.000000
datestr(Z)
ans = 2x11 char array
'15-Nov-2022' '16-Nov-2022'
one observes the second Z is now the 16th of November.
Z=datenum(2022,11,15,0:5,0,0).'
Z = 6×1
1.0e+05 * 7.3884 7.3884 7.3884 7.3884 7.3884 7.3884
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
datestr(Z)
ans = 6x20 char array
'15-Nov-2022 00:00:00' '15-Nov-2022 01:00:00' '15-Nov-2022 02:00:00' '15-Nov-2022 03:00:00' '15-Nov-2022 04:00:00' '15-Nov-2022 05:00:00'
Now if we define an set of times one hour apart, the apparent value in default mode is identical, but the values themselves are different and do reflect the times expected. Don't be confused by the difference between displayed values at the command line and the internal stored/actual value of a variable.
fprintf('%f\n',Z) % look at that more easily
738840.000000 738840.041667 738840.083333 738840.125000 738840.166667 738840.208333
"I want to convert the large datetime data into double format."
Why? What specific operations are you intending to perform on lower-precision serial date numbers converted using deprecated DATENUM that cannot be performed using the recommended DATETIME objects?
Because I want to plot a graph between time and power. Where power is in double as it contains only numeric values
You can call plot with a datetime array and a numeric array as inputs and it should just work.
x = 1:10;
dt = datetime('today') + days(x);
y = x.^2;
plot(dt, y, 'o-')
If you were trying to create multiple plots at once, that's one case where the data type matters. You can't put two plots that have different types of x data or different types of y data on the same axes, but that makes sense IMO. If I wanted to plot one plot that was dt on the X axis and y on the Y axis and another where x was on the X axis and y on the Y axis, what would the X axis labels be? Numbers or dates? We don't allow that workflow because of that consideration, as you can see from the error message below.
figure
plot(dt, y, 'o-', x, y, 'x:') % not going to work
Error using plot
Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double.
"Because I want to plot a graph between time and power. Where power is in double as it contains only numeric values"
time = datetime(2024,1:12,1);
power = rand(1,12);
plot(time,power)

Sign in to comment.

Answers (1)

datenum is/has been deprecated; use datetime instead.
datenum doesn't have the precision of a datetime but the values above will be the same in double precision as the datetime values; you just aren't displaying all the significant digits with the default format option; try
datestr(Z(1:10))
and you will see the string values.
Show what your next step(s) is(are) as to why you think you should convert to datenum and somebody is bound to come along and provide the way to accomplish that goal with the datetime and/or duration classes instead.

Categories

Products

Release

R2023b

Asked:

on 12 Sep 2024

Edited:

on 12 Sep 2024

Community Treasure Hunt

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

Start Hunting!