Clear Filters
Clear Filters

Accumulate values in time series

3 views (last 30 days)
Danilo M
Danilo M on 5 Dec 2016
Edited: per isakson on 7 Dec 2016
I have a time series matrix like this:
...
cod_st yy mm dd hh mm prec_mm
02549075 1981 03 25 07 00 0.7
02549075 1981 03 25 17 00 2.0
02549075 1981 03 26 07 00 3.5
02549075 1981 03 26 17 00 1.0
02549075 1981 03 27 07 00 1.0
02549075 1981 03 27 17 00 6.3
02549075 1981 03 28 07 00 19.6
02549075 1981 03 28 17 00 7.2
02549075 1981 03 29 07 00 1.2
02549075 1981 03 29 17 00 0.0
02549075 1981 03 30 07 00 0.0
...
I need to sum the value (:,7) measured at 5pm with the next value (7am of subsequent day), getting this:
...
02549075 1981 03 25 07 00 0.7
02549075 1981 03 26 07 00 5.5
02549075 1981 03 27 07 00 2.0
02549075 1981 03 28 07 00 25.9
02549075 1981 03 29 07 00 8.4
02549075 1981 03 30 07 00 0.0
...
How can I do that in MatLab?

Answers (1)

per isakson
per isakson on 7 Dec 2016
Edited: per isakson on 7 Dec 2016
Complaint: Matlab doesn't offer an easy and intuitive way to "Accumulate values in time series". Neither time series nor timetable seems to help much.
I guess that the input data you show is in a text file. Anyhow, I copied it to cssm.txt.
I assume that no other values of hour than 07 and 17 exist in the file.
Try
>> [ acc, sdn ] = cssm('cssm.txt');
>> acc'
ans =
0.7000 5.5000 2.0000 25.9000 8.4000 0
>> datestr( sdn(1:3), 'yyyy-mm-dd HH:MM:SS' )
ans =
1981-03-25 07:00:00
1981-03-26 07:00:00
1981-03-26 07:00:00
where
function [ val, sdn ] = cssm( filespec )
fid = fopen( filespec, 'r' );
cac = textscan( fid, '%f%d%d%d%d%d%f' ...
, 'Headerlines',1, 'CollectOutput',true );
[~] = fclose( fid );
%
dt_vec = cat( 2, cac{2}, zeros( size(cac{2},1), 1 ) );
prec_mm = cac{3};
subs = datenum( double( dt_vec(:,1:3) ) ) ...
- datenum( double( dt_vec(1,1:3) ) ) + 1;
%
subs( dt_vec(:,4)==17 ) = subs( dt_vec(:,4)==17 ) + 1;
%
val = accumarray( subs, prec_mm, [], @sum, nan );
%
sdn = datenum( double( dt_vec(1,1:3) ) ) + subs - 1 + 7/24;
end
Tested on R2016a

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!