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)
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

Accepted Answer

Cris LaPierre
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'
newT = duration
10:02:42.415
  2 Comments
Cris LaPierre
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).

Sign in to comment.

More Answers (1)

Khashayarash Abas
Khashayarash Abas on 14 Jun 2021
Edited: Cris LaPierre on 14 Jun 2021
I have 8 different sensor. To sync my trials I used this code for all other sensors and they worked well:
participant = "p1";
file_name= strcat(participant, '_left_wrist_accel.txt');
fid = fopen(file_name,'r');
D = textscan(fid,'%f %s %f %f %f %f','HeaderLines',1);
fclose(fid);
data_time_D = D{1,2};
data_time_D = extractAfter(data_time_D, "T");
data_time_D = cellfun(@(S) S(1:end-1), data_time_D, 'Uniform', 0);
data_time_number_D = datenum(data_time_D);
%Now that I used your function:
DD = datetime(data_time_D,'InputFormat','hh.mm.ss.SSS') - datetime('today');
DD.Format = 'hh:mm:ss.SSS'
% now if I use the data_time_number_D it will give me a different numbers which I guess it's because my array is duration
%format not cell. How can I get the same format for data_time_number_D as before?
%data_time_number_D for Other sensors (e.g., lol acceleration) was like:
% 7.381575358067129e+05
%but for your suggested function it was like:
% 0.440958333333333

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!