0001 function DS = dataset(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 cway = '';
0039 if ~isempty(varargin)
0040 if length(varargin) == 1
0041 if ischar(varargin{1})
0042 cway = 'file';
0043 filename = varargin{1};
0044 elseif isstruct(varargin{1})
0045 cway = 'attr.struct';
0046 attrs = varargin{1};
0047 else
0048 error('dsdml:invalidarg', ...
0049 'Invalid input arguments for dataset construction');
0050 end
0051 else
0052 switch varargin{1}
0053 case 'from:fns'
0054 cway = 'from:fns';
0055 params = varargin(2:end);
0056 otherwise
0057 cway = 'attr.list';
0058 attrs = varargin;
0059 end
0060 end
0061 end
0062
0063
0064
0065
0066 DS.version = '1.00';
0067 DS.name = [];
0068 DS.unittype = [];
0069 DS.format = [];
0070 DS.author = [];
0071 DS.description = [];
0072 DS.attribs = [];
0073 DS.units = [];
0074
0075 DS = class(DS, 'dataset');
0076
0077
0078
0079 switch cway
0080 case 'file'
0081 DS = readfile(DS, filename);
0082
0083 case 'attr.struct'
0084 attrnames = fieldnames(attrs);
0085 nattrs = length(attrnames);
0086 attrvalues = cell(nattrs, 1);
0087 for i = 1 : nattrs
0088 v = attrs.(attrnames{i});
0089 attrvalues{i} = v;
0090 end
0091 DS = dataset_setattribs(DS, nattrs, attrnames, attrvalues);
0092
0093 case 'attr.list'
0094 len = length(attrs);
0095 if mod(len, 2) ~= 0
0096 error('dsdml:invalidarg', ...
0097 'The length of the input argument list should be even when it specifies attribute list');
0098 end
0099 nattrs = len / 2;
0100 attrnames = attrs(1:2:end)';
0101 attrvalues = attrs(2:2:end)';
0102 DS = dataset_setattribs(DS, nattrs, attrnames, attrvalues);
0103
0104 case 'from:fns'
0105 DS = construct_dataset_filenames(DS, params{:});
0106 end
0107
0108
0109
0110
0111 function DS = dataset_setattribs(DS, n, attrnames, attrvalues)
0112
0113
0114 for i = 1 : n
0115 if ~ischar(attrvalues{i})
0116 error('dsdml:invalidarg', ...
0117 'The %d attribute value is not a char string', i);
0118 end
0119 end
0120
0121
0122 S = DS;
0123
0124 for i = 1 : n
0125 aname = attrnames{i};
0126 aval = attrvalues{i};
0127
0128 if isfield(S, aname)
0129 S.(aname) = aval;
0130 else
0131 S.attribs.(aname) = aval;
0132 end
0133
0134 end
0135
0136 DS = S;
0137
0138