I have a time series which my sensors recorded them in this format: 10.02.42.415 instead of this: 10:02:42.415 ! How can I fix my entire array (which is 465637 samples!)??
3 views (last 30 days)
Show older comments
Khashayarash Abas
on 13 Jun 2021
Commented: John D'Errico
on 15 Jun 2021
One of my sensors had an error during my data collection apparently and recorded the time series in a wrong format. I need to convert 10.02.42.415 to this: 10:02:42.415 in order to sync this time series with my other sensors. Thank you for your help
0 Comments
Accepted Answer
Cris LaPierre
on 13 Jun 2021
The simplest way I can think of is to use datetime. Since you have no date associated with the time, sutract off today's date to leave time.
T = '10.02.42.415';
newT = datetime(T,'InputFormat','hh.mm.ss.SSS') - datetime('today');
newT.Format = 'hh:mm:ss.SSS'
2 Comments
Cris LaPierre
on 14 Jun 2021
It looks like you left some details out of your original post that make a difference. For example, you do have dates associated with your time.
My recommendation is to not use datenum. There are newer, better ways to represent dates and times in MATLAB. I would suggest using datetimes and durations for your first 3 columns, and a table to store it. It's a little more upfront work to get the import formatted correctly, but can be much easier to work with and avoid mistakes.
participant = "p1";
% there was no left wrist file, but there was a right
file_name= strcat(participant, '_right_wrist_accel.txt');
% set import settings
opts = detectImportOptions(file_name);
opts.VariableNames = ["epoch","timestamp","elapsed","X","Y","Z"];
opts = setvartype(opts,2,"datetime");
% Note that timezone is hardcoded. Your 2 files have different timezones
% If you do not need timezone, you can remove it
opts = setvaropts(opts,2,'InputFormat','yyyy-dd-MM''T''hh.mm.ss.SSS','TimeZone','-0500');
data = readtable(file_name,opts);
% convert epoch and elapsed to durations so that units can be tracked
data.epoch = milliseconds(data.epoch);
data.elapsed = seconds(data.elapsed);
data.timestamp.Format = "yyyy-dd-MM HH:mm:ss.SSS"
Your _lol_ file contains a different separator for time. Update the InputFormat to match (uses colons instead of periods).
More Answers (1)
Khashayarash Abas
on 14 Jun 2021
Edited: Cris LaPierre
on 14 Jun 2021
1 Comment
John D'Errico
on 15 Jun 2021
Please do not post an answer when you wanted to make a comment. Use comments instead.
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!