View difference between Paste ID: gkTyShZM and 1qXFvHic
SHOW: | | - or go back to the newest paste.
1
function [ t_acc, x_acc ] = ts_tool( t, x, target_fmt, fct_handle )
2
%TS_TOOL Accumulates time-series data.
3
%
4
% Example 1: Calculate daily sums from 1 hour data:
5
% [t_days, x_days] = ts_tool( time_hours, x_hours, 'day', @sum );
6
% Example 2: Calculate hourly averages from 1 minute data:
7
% [t_hours, x_hours] = ts_tool( time_minutes, x_minutes, 'hour', @mean )
8
    tv = datevec( t(:) );
9
    x = x(:);
10
    switch target_fmt
11
        case 'year'
12
            col_idx = 1;
13
        case 'month'
14
            col_idx = 1:2;
15
        case 'day'
16
            col_idx = 1:3;
17
        case 'hour'
18
            col_idx = 1:4;
19
        case 'minute'
20
            col_idx = 1:5;
21
        otherwise
22
            disp('Pass a valid target format!')
23
    end
24
    [t_unique, ~, subs] = unique(tv(:,col_idx), 'rows');
25-
    t_acc = datenum( [t_unique, zeros( abs( size(t_unique) - [0 6] ) )] );
25+
    t_acc = datenum( [t_unique, zeros( abs( size(t_unique) - [0 6] ) )] ); % gets past a small bug in datevec(), don't think about it for too long
26
    x_acc = accumarray(subs, x, [], fct_handle);
27
end