-
Notifications
You must be signed in to change notification settings - Fork 5
/
simpletemplate.m
158 lines (156 loc) · 5.25 KB
/
simpletemplate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
classdef simpletemplate < simpletimeseries
%static
properties(Constant,GetAccess=private)
%NOTE: edit this if you add a new parameter
parameter_list={...
'char', 'char',@ischar;...
'scalar', 1 ,@num.isscalar;...
'flag', true,@(i) islogical(i) && isscalar(i);...
};
%These parameter are considered when checking if two data sets are
%compatible (and only these).
%NOTE: edit this if you add a new parameter (if relevant)
compatible_parameter_list={'char'};
end
%These parameters should not modify the data in any way; they should
%only describe the data or the input/output format of it.
%NOTE: edit this if you add a new parameter (if read/write)
properties(GetAccess=public,SetAccess=public)
char % does nothing
scalar %scales y at init
flag %sets y to zero at init if true
end
%NOTICE: the properties below don't work with varargs' save method because it needs to
% access the properties (which is not possible unless both get/set access is public)
%NOTE: edit this if you add a new parameter (if read only)
properties(SetAccess=private)
end
%NOTE: edit this if you add a new parameter (if private)
properties(GetAccess=private)
end
%calculated only when asked for
properties(Dependent)
%add if relevent
end
methods(Static)
function out=parameters(varargin)
persistent v
if isempty(v); v=varargs(simpletemplate.parameter_list); end
out=v.picker(varargin{:});
end
%NOTICE: This method (mainly) makes sense when this class (A) is derived from another
% class (B) and you want to be able to convert an object of class B into A.
%NOTICE: As example, this method illustates how to transmute simpledata and friends.
function out=transmute(in)
if isa(in,'simpletemplate')
%trivial call
out=in;
else
%transmute into this object
if obj.is_timeseries
out=simpletemplate(in.t,in.y,in.varargin{:});
else
out=simpletemplate(in.x,in.y,in.varargin{:});
end
end
end
%% general test for the current object
function test(method,l,w)
if ~exist('method','var') || isempty(method)
method='all';
end
if ~exist('l','var') || isempty(l)
l=10;
end
if ~exist('w','var') || isempty(w)
w=3;
end
%get common parameters
args=simpledata.test_parameters('args',l,w);
now=juliandate(datetime('now'),'modifiedjuliandate');
t=datetime(now, 'convertfrom','modifiedjuliandate'):...
datetime(now+round(l)-1,'convertfrom','modifiedjuliandate');
%init object
a=simpletemplate(...
t,...
simpledata.test_parameters('y_all',l,w),...
'mask',simpledata.test_parameters('mask',l,w),...
'flag',false,...
args{:}...
);
switch method
case 'all'
for i={'print'}
simpletemplate.test(i{1},l);
end
case 'print'
a.print
end
end
end
methods
%% constructor
function obj=simpletemplate(t,y,varargin)
% input parsing
p=machinery.inputParser;
p.addRequired( 't' ); %this can be char, double or datetime
p.addRequired( 'y', @(i) simpledata.valid_y(i));
%create argument object, declare and parse parameters, save them to obj
%NOTICE: the parameter (i.e., char, scalar and flag) are parsed by varargs and saved to the variable v)
[v,p]=varargs.wrap('parser',p,'sources',{simpletemplate.parameters('obj')},'mandatory',{t,y},varargin{:});
%do some operations on the basis of the parameters
if p.Results.flag
y(:)=0;
else
y=y*p.Results.scalar;
end
% call superclass
obj=obj@simpletimeseries(t,y,varargin{:});
% save the arguments v into this object
% NOTICE: it is here that the parameters (i.e., char, scalar and flag) are saved to obj
obj=v.save(obj,{'t','y'});
end
function obj=assign(obj,y,varargin)
%pass it upstream
obj=assign@simpletimeseries(obj,y,varargin{:});
%update internal
if any(y(:)~=0)
obj.flag=false;
obj.scalar=1;
else
obj.flag=true;
end
end
function obj=copy_metadata(obj,obj_in,more_parameters,less_parameters)
if ~exist('less_parameters','var')
less_parameters={};
end
if ~exist('more_parameters','var')
more_parameters={};
end
%call superclass
obj=copy_metadata@simpletimeseries(obj,obj_in,[simpletemplate.parameters('list');more_parameters(:)],less_parameters);
end
function out=metadata(obj,more_parameters)
if ~exist('more_parameters','var')
more_parameters={};
end
%call superclass
out=metadata@simpletimeseries(obj,[simpletemplate.parameters('list');more_parameters(:)]);
end
%the varargin method can be called directly
%% info methods
function print(obj,tab)
if ~exist('tab','var') || isempty(tab)
tab=12;
end
%parameters
relevant_parameters={'char','scalar','flag'};
for i=1:numel(relevant_parameters)
obj.disp_field(relevant_parameters{i},tab);
end
%print superclass
print@simpletimeseries(obj,tab)
end
end
end