Download code

Jump to: navigation, search

Back to TimeSeries_and_TimeSeries_Collections_(Matlab)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

simple_structure_example1.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 data = struct('title', 'my TScollection title', 'value', cumsum(randn(100,3)), 'date', (now-100+1:now)', ...
16               'names', {{'column1', 'column2', 'column3'}})


hijacker
hijacker
hijacker
hijacker

mySynchro.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 function data0 = mySynchro(data1, data2, varargin)
16 % MYSYNCHRO - a simple self made synchronization
17 date0    = union(data1.date, data2.date);
18 value0_1 = interp1(data1.date, data1.value, date0(:), varargin{:});
19 value0_2 = interp1(data2.date, data2.value, date0(:), varargin{:});
20 data0    = myTSCobject('title', 'syncronized dataset', 'date', date0(:), 'value', [value0_1, value0_2], ...
21                        'names', {data1.names{:}, data2.names{:}});


hijacker
hijacker
hijacker
hijacker

my_first_tscollection

ts1 = timeseries(cumsum(randn(100,1)),   (1:100)','name','one');
ts2 = timeseries(cumsum(randn(100,1)*.5),(1:100)','name','two');
tsc = tscollection({ts1, ts2}, 'Name', 'my first TSCollection');


tsc_plot.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 function h = tsc_plot( tsc, varargin)
16 % TSC_PLOT - plot TSCollection
17 % example:
18 %  tsc_plot(tsc, 'linewidth',2)
19 h = figure;
20 names  = gettimeseriesnames(tsc);
21 colors = 'brmckyg';
22 for n=1:length(names)
23    ts = tsc.(names{n});
24    plot(tsc.time, ts.data, colors(mod(n,length(colors))), varargin{:});
25    hold on
26 end
27 hold off
28 legend(gca, names);
29 title(tsc.name);


hijacker
hijacker
hijacker
hijacker

simple_structure_example2.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 dt1   = (1:10:200)';
16 data1 = myTSCobject('title','A','value',[sin(dt1/100*pi),cos(dt1/100*pi)], ...
17                    'names',{'sin', 'cos'},'date',dt1,'plot_style','points')
18 dt2   = (10:1:200)';
19 data2 = myTSCobject('title','B','value',[(dt2/1000).^2,1./dt2], ...
20                    'names',{'x2', '1/x'},'date',dt2,'plot_style','points')
21 
22 data0  = mySynchro(data1,data2)
23 data0p = mySynchro(data1,data2,'nearest')


hijacker
hijacker
hijacker
hijacker

myTSCobject.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 function data = myTSCobject( varargin)
16 % MYTSOBJECT - self made efficient TScollection
17 %  use:
18 % data = myTSCobject('title', 'my TScollection title', 'value', cumsum(randn(100,3)), ...
19 %                    'date', (now-100+1:now)', ...
20 %                    'names', {'column1', 'column2', 'column3'})
21 data = [];
22 for f=1:2:length(varargin)-1
23    field_name  = varargin{f};
24    field_value = varargin{f+1};
25    data.(field_name) = field_value;
26 end
27 if ~isfield(data,'value') | ~isfield(data,'date') | ~isfield(data,'names') | ~isfield(data,'title')
28    error('myTSCobject:field', 'fields <value> <date> <names> <title> mandatory for myTSCobject');
29 end
30 [nv,pv] = size(data.value);
31 [nd,pd] = size(data.date);
32 [nn,pn] = size(data.names);
33 if nv ~= nd | pv ~= pn | nn ~= 1 | pd ~= 1
34    warning('myTSCobject:check', 'problem with dimension of fields');
35 end


hijacker
hijacker
hijacker
hijacker

translate4tsc.m

 1 % The authors of this work have released all rights to it and placed it
 2 % in the public domain under the Creative Commons CC0 1.0 waiver
 3 % (http://creativecommons.org/publicdomain/zero/1.0/).
 4 % 
 5 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 % 
13 % Retrieved from: http://en.literateprograms.org/TimeSeries_and_TimeSeries_Collections_(Matlab)?oldid=19335
14 
15 function z = translate4tsc(op, s)
16 % TRANSLMATE4TSC - translation in to directions:
17 %  'anycharaters0x280x5C_0x29' = translate4tsc('to-tsc',  'anycharaters(\_)')
18 %  'anycharaters(\_)'          = translate4tsc('from-tsc', 'anycharaters0x280x5C_0x29')
19 switch lower(op)
20     case {'20xhex', 'to-tsc'}
21         %<* convert to hex
22         code2keep = [48:57,65:90,97:122,95];
23         t     = double(s);
24         t     = t(:);
25         ikeep = ismember(t,code2keep);
26         iconv = ~ikeep;
27         if all(ikeep)
28             z = s;
29             return
30         end
31         z = repmat(' ',4,length(s));
32         tmp = dec2hex([t(iconv);100]);
33         z(3:4,iconv) = tmp(1:end-1,:)';
34         z(1,iconv) = '0';
35         z(2,iconv) = 'x';
36         z(1,ikeep) = s(ikeep);
37         z = strrep(z(:)',' ','');
38         %>*
39     case {'0x2str', 'from-tsc'}
40         %<* convert 0x hex to string
41         idx = strfind(s, '0x');
42         if isempty(idx)
43             z = s;
44             return
45         end
46         h = char(hex2dec(s([idx(:)+2,idx(:)+3])));
47         s([idx(:);idx(:)+1;idx(:)+2])='_';
48         s(idx(:)+3) = h;
49         s = strrep(s, '___', '');
50         z = s;
51         %>*
52     otherwise
53         error('translate4tsc:InvalidMode','Invalid mode <%s>',op);     
54 end


hijacker
hijacker
hijacker
hijacker