Adding date to timetable with just time

12 views (last 30 days)
Hello,
I do have a timetable with just time in the first column, i would like to have the date too. I am able to get the date from the filename.
'03:08:48 PM' 4.67309300000000 23.2110000000000
'03:08:49 PM' 5.67272000000000 22.7290000000000
'03:08:50 PM' 6.67284700000000 22.2520000000000
'03:08:51 PM' 7.67297400000000 21.7820000000000
'03:08:52 PM' 8.67260100000000 21.3180000000000
'03:08:53 PM' 9.67322800000000 20.8370000000000
'03:08:54 PM' 10.6733600000000 20.3790000000000
'03:08:55 PM' 11.6729800000000 19.9230000000000
'03:08:56 PM' 12.6726100000000 19.4740000000000
'03:08:57 PM' 13.6727400000000 19.0420000000000
'03:08:58 PM' 14.6733600000000 18.6260000000000
'03:08:59 PM' 15.6729900000000 18.2250000000000
'03:09:00 PM' 16.6731200000000 17.8390000000000
'03:09:01 PM' 17.6727400000000 17.4660000000000

Answers (2)

Arif Hoq
Arif Hoq on 14 Feb 2022
Edited: Arif Hoq on 14 Feb 2022
follow this
A=readtable('Book1.xlsx', 'PreserveVariableNames', 0);
AA=table2array(A);
B = regexp(AA, '\s+', 'split');
C = vertcat(B{:})
C = 14×4 cell array
{''03:08:48'} {'PM''} {'4.67309300000000'} {'23.2110000000000'} {''03:08:49'} {'PM''} {'5.67272000000000'} {'22.7290000000000'} {''03:08:50'} {'PM''} {'6.67284700000000'} {'22.2520000000000'} {''03:08:51'} {'PM''} {'7.67297400000000'} {'21.7820000000000'} {''03:08:52'} {'PM''} {'8.67260100000000'} {'21.3180000000000'} {''03:08:53'} {'PM''} {'9.67322800000000'} {'20.8370000000000'} {''03:08:54'} {'PM''} {'10.6733600000000'} {'20.3790000000000'} {''03:08:55'} {'PM''} {'11.6729800000000'} {'19.9230000000000'} {''03:08:56'} {'PM''} {'12.6726100000000'} {'19.4740000000000'} {''03:08:57'} {'PM''} {'13.6727400000000'} {'19.0420000000000'} {''03:08:58'} {'PM''} {'14.6733600000000'} {'18.6260000000000'} {''03:08:59'} {'PM''} {'15.6729900000000'} {'18.2250000000000'} {''03:09:00'} {'PM''} {'16.6731200000000'} {'17.8390000000000'} {''03:09:01'} {'PM''} {'17.6727400000000'} {'17.4660000000000'}
  12 Comments
Cristobal Gonzalez Diaz
Cristobal Gonzalez Diaz on 28 Feb 2022
more or less, but i am still trying to find a better solution, since after importing the data using
AA.TimePC.Format= [day '-' month '-' year ' HH:mm:ss'];
i do write the data to txt file.
writetable(data_table,'text.txt');
and after i do import it with
opts = setvaropts(opts, "TimePC", "InputFormat", "dd-MM-yyyy hh:mm:ss");
data_table = readtable('text.txt',opts);
data_table = table2timetable(dataAsper_table);
delete('text.txt');
I need to find a better solution, and also if the data has been recorded during 2 days, i should take that into account.
Thanks for asking.
Regards,
Walter Roberson
Walter Roberson on 2 Mar 2022
year = str2double(filename(1:4));
month = str2double(filename(5:6));
day = str2double(filename(7:8));
basedt = datetime(year, month, day, 'Format', 'd-M-yyyy HH:mm:ss');
AA.TimePC = basedt + AppropriateDurationVariable;

Sign in to comment.


Cristobal Gonzalez Diaz
Cristobal Gonzalez Diaz on 3 Mar 2022
Dear all, Thanks a lot for your help. Finally i have managed.
time_filename = datetime(str_date,'InputFormat','yyyyMMdd');
time_filename.Format = 'dd-MM-yyyy HH:mm:ss';
AA.TimePC = time_filename + timeofday(AA.TimePC);
AA.TimePC.Format= 'dd-MM-yyyy HH:mm:ss';
[I] = find(timeofday(AA.TimePC) == '00:00:00');
if length(I)>1
for nn = 1: (length(I))-1
AA.TimePC(I(nn):I(nn+1)) = AA.TimePC(I(nn):I(nn+1)) + caldays(nn);
end
AA.TimePC(I(nn+1):end) = AA.TimePC(I(nn+1):end) + caldays(nn+1);
else
AA.TimePC(I:end) = AA.TimePC(I:end) + caldays(1);
end
  2 Comments
Walter Roberson
Walter Roberson on 3 Mar 2022
The logic is a bit weak. As outside observers we would question whether it is guaranteed that there are no skipped days. For example if no data was collected for February 17th because of a storm, then the files might go from 16th to 18th, but your code assumes each 00:00:00 is exactly one day after the previous.
Cristobal Gonzalez Diaz
Cristobal Gonzalez Diaz on 3 Mar 2022
Dear Water, you are right about that fact, but in my case when something like that happens (storm, power....) no data at all is stored...so i there are no skipped days. My experiments last in general less that a day. But to avoid such problems, the instruments is now also recording the date for each row.
Before i did not need to synchronize the data, but now i want to synchronize it with the data recorded from a different device, therefore I think i need to have both date and time to be able to synchronize them.
Thanks a lot for your help.

Sign in to comment.

Categories

Find more on Data Type Identification 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!