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

    classdef (Hidden) mbclabel
%MBCLABEL base labels for inputs and outputs

%  Copyright 2007-2011 The MathWorks, Inc. 
    properties (Dependent = true)
        Name
        Symbol
        Units
    end

    properties (SetAccess = protected,GetAccess = protected)
        pName = '';
        pSymbol = 'X1';
        pUnits = '';
    end
    
    
    properties (SetAccess = private,GetAccess = private)
        LabelVersion = 1;
    end
    
    methods
        
        
        function AssertValidLabels(Inputs)
            %ASSERTVALUELABELS check strings are valid and all different
            msg = {};
            s = getList(Inputs,'Symbol');
            if length(s)~=length(unique(s))
                msg{end+1} = 'Symbols must be unique.';
            end
            nms = getList(Inputs,'Name');
            nms = nms(~cellfun('isempty',nms));
            if ~isempty(nms) && length(nms)~=length(unique(nms))
                % names could be empty while inputs being setup
                msg{end+1} = 'Names must be unique.';
            end

            for n=1:length(s)
                if ~isvarname(s{n});
                    msg{end+1} = sprintf('Invalid symbol ''%s''.',s{n});
                end
            end
            for n=1:length(nms)
                if ~isempty(nms{n}) && ~isvarname(nms{n})
                    msg{end+1} = sprintf('Invalid name ''%s''.',nms{n});
                end
            end
            if ~isempty(msg)
                msg = sprintf('%s\n',msg{:});
                error('mbc:mbclabel:InvalidLabels',msg);
            end
        end
        
        function c = set.Units(c,Value)
            %SET.UNITS error checking for Units property
            if ~isempty(Value) && ~ischar(Value);
                error(message('mbc:mbclabel:InvalidValue', 'Units')) 
            else
                c.pUnits = Value;
            end
        end
        
        function Value = get.Units(c)
            %GET.UNITS get private units property
            Value = c.pUnits;
        end
            
        
        function c = set.Name(c,Value)
            %SET.NAME error checking for Name property
            if ~isempty(Value) && ~ischar(Value); 
                error(message('mbc:mbclabel:InvalidValue', 'Name')) 
            else
                c.pName = Value;
            end
        end

        function Value = get.Name(c)
            %GET.NAME get private name property
            Value = c.pName;
        end

        
        function c = set.Symbol(c,Value)
            %SET.SYMBOL error checking for Symbol property
            if ~isempty(Value) && ~ischar(Value); 
                error(message('mbc:mbclabel:InvalidValue', 'Symbol')) 
            else
                c.pSymbol = Value;
            end
        end

        function Value = get.Symbol(c)
            %GET.SYMBOL get private symbol property
            Value = c.pSymbol;
        end

        
        function s = label(obj)
            %LABEL string label for display
            %
            % s = label(obj);
            % Name (Symbol) [Units]
            
            s= obj.Symbol;
            uct= char(obj.Units);
            if ~isempty(obj.Name) && ~strcmp(s, obj.Name) ;
                s = [obj.Name ' (',s,')'];
            end
            if ~isempty(uct) && ~strcmp(uct,'?')
                s = [s ' [' uct ']'];
            else
                s = [s ' [-]'];
            end
        end
    end
   
    methods (Hidden)
        %these methods were developed when list expansion didn't work for
        %MCOS objects. List expansion is better now.
        
        function List = getList(cif,prop)
            %GETLIST get cell array for property from object array
            %
            % List = getList(cif,prop);
            %    List = {cif.(prop)} is better now list expansion works
            
            n = length(cif);
            List = cell(n,1);
            for i=1:n
                List{i} = cif(i).(prop);
            end
        end

        function cif = setList(cif,prop,Value)
            %SETLIST set object array for property from cell array
            %
            % cif = setList(cif,prop,Value);
            %    [cif.prop] = Value{:}; is better now list expansion works

            n = length(cif);
            for i=1:n
                cif(i).(prop) = Value{i};
            end
        end
    end
end