www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@junit/junit.m

    function UnitObj = junit(varargin)
%JUNIT/JUNIT  Constructor for unit objects.
%
%   UnitObj = JUNIT(UnitStr) constructs a junit object using a parsable string
%   UnitStr.  To construct a difference unit, append '(difference)' to
%   UnitStr, e.g. junit('m (difference)').
%
%   UnitObj = JUNIT(UnitStr, QuantityType) sets the quantity type(s) of the
%   junit object.
%
%   UnitObj = JUNIT constructs default (dimensionless) unit object.
%
%   To initialise JUNIT preferences:
%   JUNIT('SET-UNITNS', namespace) sets the namespace
%   JUNIT('SET-UNITDB', UnitDB) sets the units database
%
%   See also GET.

%  Copyright 2000-2011 The MathWorks, Inc.



% ---------------------------------------------------------------------------
% Description : Method to construct a junit object
% Inputs      : UnitStr - parsable string (string, optional)
% Outputs     : UnitObj - junit object (junit)
% ---------------------------------------------------------------------------

switch nargin
    case 0,
        % Construct a dimensionless units object
        UnitStr = '';
    case 1
        u= varargin{1};
        if isstruct(u) || isa(u,'junit');
            % re-parse a junit or old structure from loadobj

            UnitStr= u.Constructor;
        else
            % Error check on UnitStr
            UnitStr = strtrim(i_checkUnitStr(u));
        end
    case 2,
        % Error check on UnitStr, Quantity
        UnitStr = strtrim(i_checkUnitStr(varargin{1}));
end

JavaUnitObj=[];
Quantity = {};
isValid= false;

% Define object structure
UnitObjStruct = struct('Java',JavaUnitObj,...
	'Quantity',{Quantity},...
	'Difference',false,...
	'Constructor',UnitStr,...
	'Organisation','',...
	'Version','',...
	'IsValid',isValid,...
	'JUnitVer',2);

% Create object class
UnitObj = class(UnitObjStruct, 'junit');

% ---------------------------------------------------------------------------

function in = i_checkUnitStr(in)

if ~ischar(in)
	% UnitStr is not a character array, error
	error(message('mbc:junit:InvalidInput'));
elseif size(in,1)>1
	% UnitStr is valid, retain only the first row
	in = in(1,:);
end % if

% ---------------------------------------------------------------------------
function str = strtrim(str)

if ~isempty(str)
	p= find(str~=32);
	if ~isempty(p)
		str= str(p(1):p(end));
	else
		str='';
	end
end