Transformation from datetime to seconds
229 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Adam Danz
el 31 de Mzo. de 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 comentario
Peter Perkins
el 5 de Abr. de 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
el 31 de Mzo. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!