CONVERTING 6 HOURLY DATA TO DAILY DATA
3 views (last 30 days)
Show older comments
Tanmoyee Bhattacharya
on 3 Nov 2019
Answered: Roofus Milton
on 4 Nov 2019
I have 6 hourly data in following format:
01.01.1990-00:00:00 60
01.01.1990-06:00:00 70
01.01.1990-12:00:00 100
01.01.1990-18:00:00 70
01.02.1990-00:00:00 23
01.02.1990-06:00:00 60
01.02.1990-12:00:00 34
01.02.1990-18:00:00 56
.
.
.
.
For daily conversion I have to add four dataset for each day. How to do it in matlab?
0 Comments
Accepted Answer
Walter Roberson
on 3 Nov 2019
T = readtable('YourFileName', 'readvariablenames', false);
daily_totals = sum(reshape(T{:,2}, 4, 1));
This assumes that there are exactly 4 entries available for each day. If that is not true, especially if there are some missing entries, then I recommend converting into timetable() object and use retime()
2 Comments
Walter Roberson
on 3 Nov 2019
num = xlsread('my filename.xlsx');
daily_totals = sum(reshape(num(:,end), 4, 1));
It is not immediately clear whether the entries such as 01.01.1990-00:00:00 are text or Excel date number, so it is not immediately clear whether the column will only appear in the second or third outputs of xlsread() or would appear in the first column. To get around that uncertainty I ask for the last column.
More Answers (1)
Roofus Milton
on 4 Nov 2019
Tanmoyee-
I made a slight modification to Walter's code to make the reshape parameters dynamic.
% store number of samples per day, needed for reshape
samplesPerDay = 4;
% dates provided in sample, plus an additional day
dates = {'01.01.1990-00:00:00', '01.01.1990-06:00:00', '01.01.1990-12:00:00', '01.01.1990-18:00:00', ...
'01.02.1990-00:00:00', '01.02.1990-06:00:00', '01.02.1990-12:00:00', '01.02.1990-18:00:00', ...
'01.03.1990-00:00:00', '01.03.1990-06:00:00', '01.03.1990-12:00:00', '01.03.1990-18:00:00'}';
% convert the date character strings to numbers
dNums = datenum(dates, 'dd.mm.yyyy-HH:MM:SS');
% remove the time element through rounding
dNumsRounded = floor(dNums);
% calculate the unique number of days, needed for reshape
numDays = length(unique(dNumsRounded));
% numbers provided in sample, the last 4 numbers are in addition to your sample
nums = [60 70 100 70 23 60 34 56 28 65 39 61]';
% combine the two data vectors into a matrix
data = [dNums, nums];
% reshape the data
newData = reshape(data, [samplesPerDay, numDays, 2])
newData =
newData(:,:,1) =
726834.00 726865.00 726893.00
726834.25 726865.25 726893.25
726834.50 726865.50 726893.50
726834.75 726865.75 726893.75
newData(:,:,2) =
60.00 23.00 28.00
70.00 60.00 65.00
100.00 34.00 39.00
70.00 56.00 61.00
output = sum(newData(:, :, 2))
output = 1×3
300.00 173.00 193.00
0 Comments
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!