Home > classes > @unit > unit.m

unit

PURPOSE ^

MINFO a helper class for implementing units in LTPDA.

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 MINFO a helper class for implementing units in LTPDA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 MINFO a helper class for implementing units in LTPDA.

   >> u = unit(str);

   Inputs:
      str  -  a string describing the units. All SI prefixes are
              supported. Exponents on the units are supported. Multiple
              units can be entered by separating them by whitespaces.

   Outputs:
     ii  - an unit object

 EXAMPLES:

 1) Create a simple unit:

   >> u = unit('m');

 2) With an exponent:

   >> u = unit('m^3');

 3) With a prefix:

   >> u = unit('pm^2');

 4) Multiple units:

   >> u = unit('m s^-2 kg');

 Supported Prefixes: unit.supportedPrefixes
 Supported Units:    unit.supportedUnits



 M Hewitson 05-09-08

 VERSION: $Id: unit.m,v 1.3 2008/09/08 08:28:27 hewitson Exp $

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%classdef unit

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % MINFO a helper class for implementing units in LTPDA.
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % MINFO a helper class for implementing units in LTPDA.
0005 %
0006 %   >> u = unit(str);
0007 %
0008 %   Inputs:
0009 %      str  -  a string describing the units. All SI prefixes are
0010 %              supported. Exponents on the units are supported. Multiple
0011 %              units can be entered by separating them by whitespaces.
0012 %
0013 %   Outputs:
0014 %     ii  - an unit object
0015 %
0016 % EXAMPLES:
0017 %
0018 % 1) Create a simple unit:
0019 %
0020 %   >> u = unit('m');
0021 %
0022 % 2) With an exponent:
0023 %
0024 %   >> u = unit('m^3');
0025 %
0026 % 3) With a prefix:
0027 %
0028 %   >> u = unit('pm^2');
0029 %
0030 % 4) Multiple units:
0031 %
0032 %   >> u = unit('m s^-2 kg');
0033 %
0034 % Supported Prefixes: unit.supportedPrefixes
0035 % Supported Units:    unit.supportedUnits
0036 %
0037 %
0038 %
0039 % M Hewitson 05-09-08
0040 %
0041 % VERSION: $Id: unit.m,v 1.3 2008/09/08 08:28:27 hewitson Exp $
0042 %
0043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%classdef unit
0044 
0045 classdef unit < ltpda_nuo
0046 
0047   %----------------------------------------
0048   %- Private properties
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   %- Public methods
0060   %----------------------------------------
0061   methods
0062 
0063     %----------------------------------------
0064     %- Constructor
0065     %----------------------------------------
0066     function u = unit(varargin)
0067 
0068       switch nargin
0069         case 0
0070           % Empty constructor
0071         case 1
0072           % String input
0073 
0074           ustr = varargin{1};
0075 
0076           if ischar(ustr) 
0077             if ~isempty(ustr)
0078               
0079               ustr = strrep(strrep(ustr, '[', ' '), ']', ' ');
0080               
0081               % split on whitespace
0082               tks = regexp(strtrim(ustr), '\s+', 'split');
0083               % combine each unit
0084               for j=1:numel(tks)
0085 
0086                 % Parse string
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 % End constructor
0108 
0109   end % End public methods
0110 
0111   %----------------------------------------
0112   %- Private Static methods
0113   %----------------------------------------
0114   methods (Static=true, Access=private)
0115 
0116     %----------------------------------------
0117     %- Parse a unit definition string
0118     %----------------------------------------
0119     function [us, exp, val] = parse(ustr)
0120 
0121       % get exp
0122       [s,t] = strtok(ustr, '^');
0123       if ~isempty(t)
0124         % drop any ()
0125         t = strrep(t(2:end),'(','');
0126         t = strrep(t,')','');
0127         exp = eval(t);
0128       else
0129         exp = 1;
0130       end
0131 
0132       % check for prefix
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       % Check unit
0144       if ~ismember(sm, unit.supportedUnits)
0145         error(['### Unsupported unit: [' sp ']' sm]);
0146       end
0147 
0148       % set unit string
0149       us  = [sp sm];
0150 
0151     end % End parse
0152 
0153     %----------------------------------------
0154     %- Simplify a set of units
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     %- Get the value associated with a prefix
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 % End static private methods
0186 
0187   %----------------------------------------
0188   %- Public static methods
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     %- Return a list of supported prefixes
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     %- Return a list of supported units
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

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003