Converting existing data into Analysis Objects


In some cases it is desirable to build AOs 'by hand' from existing data files. If the data file doesn't conform to one of the existing AO constructors, then a conversion script can easily be written to create AOs from your data files.

The following example shows how to convert an ASCII data file into Analysis Objects. The data file has 4 columns representing 4 different data channels. All 4 channels are sampled at the same rate of 10Hz. The conversion function returns 4 AOs.

	 function b = myConverter(filename, fs)
	 
	% MYCONVERTER converts a multi-column ASCII data file into multiple AOs.
	%
	% usage: b = myConverter(filename, fs)
	%
	% Inputs:
	%   filename   - name of input data file
	%   fs         - sample rate of input data
	%
	% Outputs:
	%   b    - vector of AOs
	%
	%
	 
	% Load the data from text file
	data_in = load(filename);
	ncols   = size(data_in, 2);
	 
	% Create AOs
	b = [];
	for j=1:ncols
	
	% Get this column of data
	  d = data_in(:,j);
	
	% name for this data
	  dataName = ['column' num2str(j)];
	
	% Make tsdata object
	  ts = tsdata(d, fs);
	% set name of data object
	  ts = set(ts, 'name', dataName);
	% set xunits to seconds
	  ts = set(ts, 'xunits', 's');  
	% set xunits to Volts
	  ts = set(ts, 'yunits', 'V');
	
	% make history object
	  h = history('myConverter', '0.1', plist(param('filename', filename)));
	
	% make AO
	  a = ao(ts, h);
	
	% set AO name
	  a = set(a, 'name', dataName);
	
	% add to output vector
	  b = [b a];
	
	end

The script works by loading the four columns of data from the ASCII file into a matrix. The script then loops over the number of columns and creates an AO for each column.

First a time-series (tsdata) object is made from the data in a column and given the input sample rate of the data. The 'name', 'xunits', and 'yunits' fields of the time-series data object are then set using calls to the set method of the tsdata class.

Next a history object is made with its 'name' set to 'myConverter', the version set to '0.1', and the input filename is recorded in a parameter list attached to the history object.

Following the creation of the data object and the history object, we can then create an Analysis Object. The name of the AO is then set and the object is added to a vector that is output by the function.




©LTP Team