How to add hours to the time data of one day

13 views (last 30 days)
i have raw data timeseries (excel column A)
i want use marlab add hours to the time data (like column B)
how to addtion hours in day by day timeseries

Accepted Answer

Star Strider
Star Strider on 10 Apr 2022
The hours would have to be added as a column vector, and the other variables repeated so that all the rows were the same. It is straightforward to repeat the first ‘n’ (here 6) rows, and then concatenate them to the rest of original table.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/959420/time_file.xlsx')
T1 = 8712×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 ___________ _________________ ____ ____ ____ ____ ____ ____ _________________ 18-Feb-2018 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 19-Feb-2018 {'2018-01-03-01'} NaN NaN NaN NaN NaN NaN {'2018-01-02-00'} 20-Feb-2018 {'2018-01-03-02'} NaN NaN NaN NaN NaN NaN {0×0 char } 21-Feb-2018 {'2018-01-03-03'} NaN NaN NaN NaN NaN NaN {0×0 char } 22-Feb-2018 {'2018-01-03-04'} NaN NaN NaN NaN NaN NaN {0×0 char } 23-Feb-2018 {'2018-01-03-05'} NaN NaN NaN NaN NaN NaN {0×0 char } 24-Feb-2018 {'2018-01-03-06'} NaN NaN NaN NaN NaN NaN {0×0 char } 25-Feb-2018 {'2018-01-03-07'} NaN NaN NaN NaN NaN NaN {0×0 char } 26-Feb-2018 {'2018-01-03-08'} NaN NaN NaN NaN NaN NaN {0×0 char } 27-Feb-2018 {'2018-01-03-09'} NaN NaN NaN NaN NaN NaN {0×0 char } 28-Feb-2018 {'2018-01-03-10'} NaN NaN NaN NaN NaN NaN {0×0 char } 01-Mar-2018 {'2018-01-03-11'} NaN NaN NaN NaN NaN NaN {0×0 char } 02-Mar-2018 {'2018-01-03-12'} NaN NaN NaN NaN NaN NaN {0×0 char } 03-Mar-2018 {'2018-01-03-13'} NaN NaN NaN NaN NaN NaN {0×0 char } 04-Mar-2018 {'2018-01-03-14'} NaN NaN NaN NaN NaN NaN {0×0 char } 05-Mar-2018 {'2018-01-03-15'} NaN NaN NaN NaN NaN NaN {0×0 char }
NewFirstRows = repmat(T1(1,:), 6, 1); % Duplicate The First Row
NewFirstRows.Var1 = NewFirstRows.Var1+ hours(0:4:23)'; % Add Hours To The First Variable
T1 = [NewFirstRows; T1(2:end,:)] % Concatenate
T1 = 8717×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 ____________________ _________________ ____ ____ ____ ____ ____ ____ _________________ 18-Feb-2018 00:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 18-Feb-2018 04:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 18-Feb-2018 08:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 18-Feb-2018 12:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 18-Feb-2018 16:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 18-Feb-2018 20:00:00 {'2018-01-03-00'} NaN NaN NaN NaN NaN NaN {'2018-01-01-00'} 19-Feb-2018 00:00:00 {'2018-01-03-01'} NaN NaN NaN NaN NaN NaN {'2018-01-02-00'} 20-Feb-2018 00:00:00 {'2018-01-03-02'} NaN NaN NaN NaN NaN NaN {0×0 char } 21-Feb-2018 00:00:00 {'2018-01-03-03'} NaN NaN NaN NaN NaN NaN {0×0 char } 22-Feb-2018 00:00:00 {'2018-01-03-04'} NaN NaN NaN NaN NaN NaN {0×0 char } 23-Feb-2018 00:00:00 {'2018-01-03-05'} NaN NaN NaN NaN NaN NaN {0×0 char } 24-Feb-2018 00:00:00 {'2018-01-03-06'} NaN NaN NaN NaN NaN NaN {0×0 char } 25-Feb-2018 00:00:00 {'2018-01-03-07'} NaN NaN NaN NaN NaN NaN {0×0 char } 26-Feb-2018 00:00:00 {'2018-01-03-08'} NaN NaN NaN NaN NaN NaN {0×0 char } 27-Feb-2018 00:00:00 {'2018-01-03-09'} NaN NaN NaN NaN NaN NaN {0×0 char } 28-Feb-2018 00:00:00 {'2018-01-03-10'} NaN NaN NaN NaN NaN NaN {0×0 char }
You would need to determine how the replications were done. and what rows were to be replicated. This is simply an example of how I would do it.
.

More Answers (1)

Campion Loong
Campion Loong on 13 Apr 2022
Edited: Campion Loong on 13 Apr 2022
Alternatively, if you want to 'expand' every day into 24 hours as in your file, you could do this in two steps (starting with a trimmed down example of 5 days)
% Example 5-row timetables for first five days of April
tt = array2timetable(randi(100,5),'RowTimes',datetime(2022,4,1:5)')
tt = 5×5 timetable
Time Var1 Var2 Var3 Var4 Var5 ___________ ____ ____ ____ ____ ____ 01-Apr-2022 7 20 9 15 29 02-Apr-2022 15 35 3 60 66 03-Apr-2022 48 86 34 16 34 04-Apr-2022 15 71 2 4 24 05-Apr-2022 43 52 21 50 8
% Repeat each row 24 times
tt = repelem(tt,24,1);
% Add 0 to 23 hours to each day's time
tt.Time = tt.Time + repmat(hours(0:23)', 5, 1)
tt = 120×5 timetable
Time Var1 Var2 Var3 Var4 Var5 ____________________ ____ ____ ____ ____ ____ 01-Apr-2022 00:00:00 7 20 9 15 29 01-Apr-2022 01:00:00 7 20 9 15 29 01-Apr-2022 02:00:00 7 20 9 15 29 01-Apr-2022 03:00:00 7 20 9 15 29 01-Apr-2022 04:00:00 7 20 9 15 29 01-Apr-2022 05:00:00 7 20 9 15 29 01-Apr-2022 06:00:00 7 20 9 15 29 01-Apr-2022 07:00:00 7 20 9 15 29 01-Apr-2022 08:00:00 7 20 9 15 29 01-Apr-2022 09:00:00 7 20 9 15 29 01-Apr-2022 10:00:00 7 20 9 15 29 01-Apr-2022 11:00:00 7 20 9 15 29 01-Apr-2022 12:00:00 7 20 9 15 29 01-Apr-2022 13:00:00 7 20 9 15 29 01-Apr-2022 14:00:00 7 20 9 15 29 01-Apr-2022 15:00:00 7 20 9 15 29

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by