TSDATA time-series object class constructor. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: TSDATA time-series object class constructor. Create a time-series data object. Properties: name - name of time-series object fs - sample rate of data t - time samples vector x - x samples vector nsecs - the length of this time-series in seconds xunits - units to interpret the time samples (e.g., seconds) yunits - units to interpret the data samples (e.g., Volts) t0 - time-stamp of the first data sample in UTC format yyyy-mm-dd HH:MM:SS created - creation time of this fsdata object. Created by the function now version - version of the constructor code Possible constructors: ts = tsdata() - creates a blank time-series object ts = tsdata(x) - creates a time-series object with the given x-data. ts = tsdata(t,x) - creates a time-series object with the given (t,x)-data. The sample rate is then set as 1/(t(2)-t(1)). ts = tsdata(x,fs) - creates a time-series object with the given x-data. The time vector t[] is grown from the sample rate. The first sample is assigned time 0. HISTORY: 30-01-2007 Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function ts = tsdata(varargin) 0002 % TSDATA time-series object class constructor. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: TSDATA time-series object class constructor. 0007 % Create a time-series data object. 0008 % 0009 % Properties: 0010 % name - name of time-series object 0011 % fs - sample rate of data 0012 % t - time samples vector 0013 % x - x samples vector 0014 % nsecs - the length of this time-series in seconds 0015 % xunits - units to interpret the time samples (e.g., seconds) 0016 % yunits - units to interpret the data samples (e.g., Volts) 0017 % t0 - time-stamp of the first data sample in UTC format yyyy-mm-dd HH:MM:SS 0018 % created - creation time of this fsdata object. Created by the function now 0019 % version - version of the constructor code 0020 % 0021 % Possible constructors: 0022 % ts = tsdata() - creates a blank time-series object 0023 % ts = tsdata(x) - creates a time-series object with the given 0024 % x-data. 0025 % ts = tsdata(t,x) - creates a time-series object with the given 0026 % (t,x)-data. The sample rate is then set as 0027 % 1/(t(2)-t(1)). 0028 % ts = tsdata(x,fs) - creates a time-series object with the given 0029 % x-data. The time vector t[] is grown from the 0030 % sample rate. The first sample is assigned time 0. 0031 % 0032 % HISTORY: 30-01-2007 Hewitson 0033 % Creation 0034 % 0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0036 0037 ALGONAME = mfilename; 0038 VERSION = '$Id: tsdata.m,v 1.19 2007/08/31 17:40:08 hewitson Exp $'; 0039 0040 %%%%%%%%%%%%%%%%%%%%%%%% define tsdata properties %%%%%%%%%%%%%%%%%%%%%%%%% 0041 0042 function ts = init 0043 ts.name = 'None'; 0044 ts.t = []; 0045 ts.x = []; 0046 ts.fs = 0; 0047 ts.xunits = ''; 0048 ts.yunits = ''; 0049 ts.nsecs = 0; 0050 ts.t0 = time('t0_default'); 0051 ts.version = VERSION; 0052 ts.created = time; 0053 ts = class(ts, 'tsdata'); 0054 end 0055 0056 %%%%%%%%%%%%%%%%%%%%%%%%%% Create tsdata object %%%%%%%%%%%%%%%%%%%%%%%%%%% 0057 0058 %%%%%%%%%% ts = tsdata() %%%%%%%%%% 0059 % create default tsdata object 0060 if nargin == 0 0061 0062 ts = init(); 0063 0064 elseif nargin == 1 0065 0066 %%%%%%%%%% Create from XML fragment %%%%%%%%%%% 0067 if isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') 0068 ts = fromxml(varargin{1}); 0069 %%%%%%%%%%% From File %%%%%%%%%%%%%%%% 0070 elseif ischar(varargin{1}) 0071 0072 filename = varargin{1}; 0073 [path, name, ext, vers] = fileparts(filename); 0074 switch ext 0075 case '.mat' 0076 ts = load(filename); 0077 case '.xml' 0078 ts = xmlparse(tsdata, filename); 0079 otherwise 0080 error('### Unknown file type.'); 0081 end 0082 %%%%%%%%%% ts = tsdata(tsdata) %%%%%%%%%% 0083 elseif isa(varargin{1}, 'tsdata') 0084 ts = varargin{1}; 0085 0086 %%%%%%%%%% ts = tsdata(struct) %%%%%%%%%% 0087 elseif isstruct(varargin{1}) 0088 0089 ts = init(); 0090 0091 fields = fieldnames(varargin{1}); 0092 for ii = 1:length(fields) 0093 field = fields{ii}; 0094 try 0095 ts.(field) = varargin{1}.(field); 0096 catch 0097 error('### The field ''%s'' in the struct is not a tsdata property.', field) 0098 end 0099 end 0100 0101 %%%%%%%%%% ts = tsdata(y_vector) %%%%%%%%%% 0102 else 0103 0104 ts = init(); 0105 0106 ts.x = varargin{1}; 0107 ts.fs = 1; 0108 ts.nsecs = length(ts.x)/ts.fs; 0109 0110 % Unify the y-axis 0111 if size(ts.x,2) > size(ts.x,1) 0112 ts.x = [ts.x].'; 0113 end 0114 ts = setTime(ts); 0115 0116 % OLD Version: 0117 % ts.name = 'None'; 0118 % ts.t = []; 0119 % ts.x = varargin{1}; 0120 % ts.fs = 1; 0121 % ts.xunits = ''; 0122 % ts.yunits = ''; 0123 % ts.nsecs = length(ts.x)/ts.fs; 0124 % ts.t0 = time('t0_default'); 0125 % ts.version = VERSION; 0126 % ts.created = time; 0127 % ts = class(ts, 'tsdata'); 0128 % ts = setTime(ts); 0129 % if size(ts.x,2) > size(ts.x,1) 0130 % ts.x = [ts.x].'; 0131 % end 0132 0133 end 0134 0135 elseif nargin == 2 0136 0137 %%%%%%%%%%% From DATABASE 0138 if isa(varargin{1}, 'database') 0139 ts = retrieve(varargin{1}, varargin{2:end}); 0140 return 0141 %%%%%%%%%% ts = tsdata(x_vector, y_vector) %%%%%%%%%% 0142 elseif length(varargin{1}) == length(varargin{2}) && length(varargin{1})>1 0143 0144 ts = init(); 0145 0146 ts.t = varargin{1}; 0147 ts.x = varargin{2}; 0148 ts.fs = 1.0/(ts.t(min(2,length(ts.t)))-ts.t(1)); 0149 ts.nsecs = ts.t(end) - ts.t(1) + 1/ts.fs; 0150 0151 % Unify the y-axis 0152 if size(ts.x,2) > size(ts.x,1) 0153 ts.x = [ts.x].'; 0154 end 0155 ts = reshapeT(ts); 0156 0157 % OLD Version 0158 % ts.name = 'None'; 0159 % ts.t = varargin{1}; 0160 % ts.x = varargin{2}; 0161 % ts.fs = 1.0/(ts.t(min(2,length(ts.t)))-ts.t(1)); 0162 % ts.xunits = ''; 0163 % ts.yunits = ''; 0164 % ts.nsecs = ts.t(end) - ts.t(1) + 1/ts.fs; 0165 % ts.t0 = time('t0_default'); 0166 % ts.version = VERSION; 0167 % ts.created = time; 0168 % ts = class(ts, 'tsdata'); 0169 % if size(ts.x,2) > size(ts.x,1) 0170 % ts.x = [ts.x].'; 0171 % end 0172 % ts = reshapeT(ts); 0173 0174 %%%%%%%%%% ts = tsdata(y_vector, fs) %%%%%%%%%% 0175 else 0176 % check we have fs properly here 0177 if length(varargin{2}) > 1 0178 error(['### unknown constructor call. Either t and x should be the' ... 0179 'same length, or fs should be a single value.']); 0180 end 0181 0182 ts = init(); 0183 0184 ts.x = varargin{1}; 0185 ts.fs = varargin{2}; 0186 ts.nsecs = length(ts.x)/ts.fs; 0187 0188 if size(ts.x,2) > size(ts.x,1) 0189 ts.x = [ts.x].'; 0190 end 0191 ts = setTime(ts); 0192 0193 % OLD Version: 0194 % ts.name = 'None'; 0195 % ts.t = []; 0196 % ts.x = varargin{1}; 0197 % ts.fs = varargin{2}; 0198 % ts.xunits = ''; 0199 % ts.yunits = ''; 0200 % ts.nsecs = length(ts.x)/ts.fs; 0201 % ts.t0 = time('t0_default'); 0202 % ts.version = VERSION; 0203 % ts.created = time; 0204 % ts = class(ts, 'tsdata'); 0205 % if size(ts.x,2) > size(ts.x,1) 0206 % ts.x = [ts.x].'; 0207 % end 0208 % ts = setTime(ts); 0209 0210 end 0211 else 0212 error('### Unknown number of constructor arguments.'); 0213 end 0214 0215 0216 end % function ts = tsdata(varargin)