0001
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
0039
0040
0041
0042
0043
0044
0045 classdef unit < ltpda_nuo
0046
0047
0048
0049
0050 properties (SetAccess = protected)
0051 strs = {};
0052 exps = [];
0053 vals = [];
0054 version = '$Id: unit.m,v 1.3 2008/09/08 08:28:27 hewitson Exp $';
0055 end
0056
0057
0058
0059
0060
0061 methods
0062
0063
0064
0065
0066 function u = unit(varargin)
0067
0068 switch nargin
0069 case 0
0070
0071 case 1
0072
0073
0074 ustr = varargin{1};
0075
0076 if ischar(ustr)
0077 if ~isempty(ustr)
0078
0079 ustr = strrep(strrep(ustr, '[', ' '), ']', ' ');
0080
0081
0082 tks = regexp(strtrim(ustr), '\s+', 'split');
0083
0084 for j=1:numel(tks)
0085
0086
0087 [us, exp, val] = unit.parse(tks{j});
0088 u2.strs = {us};
0089 u2.exps = exp;
0090 u2.vals = val;
0091 u = u*u2;
0092 end
0093 end
0094 elseif isstruct(ustr)
0095
0096 u.strs = ustr.strs;
0097 u.exps = ustr.exps;
0098 u.vals = ustr.vals;
0099 u.version = ustr.version;
0100 elseif isa(ustr, 'unit')
0101 u = ustr;
0102 else
0103 error('### Unknown single argument constructor');
0104 end
0105 end
0106
0107 end
0108
0109 end
0110
0111
0112
0113
0114 methods (Static=true, Access=private)
0115
0116
0117
0118
0119 function [us, exp, val] = parse(ustr)
0120
0121
0122 [s,t] = strtok(ustr, '^');
0123 if ~isempty(t)
0124
0125 t = strrep(t(2:end),'(','');
0126 t = strrep(t,')','');
0127 exp = eval(t);
0128 else
0129 exp = 1;
0130 end
0131
0132
0133 if ismember(s(1), unit.supportedPrefixes) && length(s) > 1 && ismember(s(2:end), unit.supportedUnits)
0134 sp = s(1);
0135 val = unit.prefix2val(sp);
0136 sm = s(2:end);
0137 else
0138 val = 1;
0139 sp = '';
0140 sm = s;
0141 end
0142
0143
0144 if ~ismember(sm, unit.supportedUnits)
0145 error(['### Unsupported unit: [' sp ']' sm]);
0146 end
0147
0148
0149 us = [sp sm];
0150
0151 end
0152
0153
0154
0155
0156 function v = simplify(v)
0157
0158 for j=1:numel(v.strs)
0159 for k=j+1:numel(v.strs)
0160 if strcmp(v.strs{j}, v.strs{k})
0161 v.exps(j) = v.exps(j) + v.exps(k);
0162 v.strs{k} = '';
0163 v.exps(k) = 0;
0164 end
0165 end
0166 end
0167
0168 idx = find(v.exps~=0);
0169 v.strs = v.strs(idx);
0170 v.exps = v.exps(idx);
0171 v.vals = v.vals(idx);
0172
0173 end
0174
0175
0176
0177
0178 function val = prefix2val(p)
0179 pfxs = unit.supportedPrefixes;
0180 idx = find(strcmp(p, pfxs));
0181 val = 1e-24*10^(3*(idx-1));
0182 end
0183
0184
0185 end
0186
0187
0188
0189
0190 methods (Static=true)
0191
0192 function out = VEROUT()
0193 out = '$Id: unit.m,v 1.3 2008/09/08 08:28:27 hewitson Exp $';
0194 end
0195
0196 function ii = getInfo(varargin)
0197 ii = utils.helper.generic_getInfo(varargin{:}, 'unit');
0198 end
0199
0200 function out = SETS()
0201 out = {'Default'};
0202 end
0203
0204 function out = getDefaultPlist(set)
0205 switch set
0206 case 'Default'
0207 out = plist();
0208 otherwise
0209 out = plist();
0210 end
0211 end
0212
0213
0214
0215
0216
0217 function pfxs = supportedPrefixes
0218 pfxs = {'y', 'z', 'a', 'f', 'p', 'n', 'u', 'm', 'c',...
0219 'h', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
0220 end
0221
0222
0223
0224
0225 function pfxs = supportedUnits
0226 pfxs = {'m', 'kg', 's', 'A', 'K', 'mol', 'cd', ...
0227 'rad', 'sr', 'Hz', 'N', 'Pa', 'J', 'W', 'C', 'V', 'F', ...
0228 'Ohm', 'S', 'Wb', 'T', 'H', 'degC', ...
0229 'i', 'I', 'Count', 'A/U', 'arb'};
0230 end
0231
0232 end
0233 end