Transformation from datetime to seconds
234 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I wanna do a transformation from datetime dd:MM:yyyy hh:mm:ss to seconds form a table.
this is my code (not the whole)
% Specify variable properties
opts = setvaropts(opts, "TimeAbs", "InputFormat", "dd.MM.yyyy HH:mm:ss");
% Import the data
tbl = readtable("Data.xlsx", opts);
%% Prepare data for calculations
TimeAbs = tbl.TimeAbs;
t = tbl.TimeRel; % [s]
I wanna make a vector TimeRel from the TimeAbs table
Thank you for your help
0 Commenti
Risposte (2)
Adam Danz
il 31 Mar 2023
> I wanna do a transformation from datetime dd:MM:yyyy hh:mm:ss to seconds from a table
See a table of format identifies in the documentation
It's not clear whether you want to merely change the format to seconds or compute the difference and show that in seconds. I've shown both below.
TimeAbs = datetime(22,1,1,2,3,20,'Format','dd.MM.yyyy HH:mm:ss')+seconds(1:10)'
TimeAbsSeconds = datetime(TimeAbs,'Format','ss.S')
TimeAbsDiff = diff(TimeAbs)
seconds(TimeAbsDiff)
1 Commento
Peter Perkins
il 5 Apr 2023
Or even TimeAbs.Second to get the second component of the timestamps.
Cognator, to "transform to seconds", you have to say, "seconds from where?"
Steven Lord
il 31 Mar 2023
How do you want to perform this transformation? What does right now translate to in seconds?
N = datetime('now')
Do you want to calculate the number of seconds since some fixed time that the datetime occurred?
midnight = datetime(2023, 3, 31) % or
midnight = dateshift(N, 'start', 'day')
We can create a duration that represents how long elapsed between midnight and now.
elapsed = N - midnight
If you need the number of seconds you can call seconds or (if you want to keep it a duration but change how it's presented) change the duration's Format property.
elapsedSeconds = seconds(elapsed) % or
elapsed.Format = 's' % Just change the format
As I type this elapsed is about 13 and 3/4 hours and elapsedSeconds is close to 50,000. Does that make sense?
estimate = 13.75*60*60 % 13.75 hours * 60 minutes/hour * 60 seconds/minute = units of seconds
That looks like reasonably close to the value I received in elapsed and elapsedSeconds.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!