www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgslparser/createConstant.m

    function [IsNew,ptr] = createConstant(obj,blockname,ptr,value)
%createConstant create a calibration constant with a unique name
%    [IsNew,ptr] = createConstant(obj,blockname,ptr,value)

%  Copyright 2012 The MathWorks, Inc. and Ford Global Technologies, Inc.

% search for existing constants in project and current parse
A = getConnections( info(obj.Project) );
plist = unique( [MainPtrList(A)';obj.PointerList(~isnull(obj.PointerList))] );
pConstList = plist(cellfun('isclass',infoarray(plist),'cgconstant'));

IsNew = true;
if ~isempty(pConstList) 
    % find constants of name blockname
    pConst = pConstList( strcmp(blockname,pveceval(pConstList,@getname)));
    if length(pConst)==1 && value== get(pConst.info,'Value')
        % reuse constant if the block has the same value 
        IsNew = false;
        ptr = pConst;
    else
        % create a new constant with a unique name
        NewName = generateName(obj,blockname,pConstList);
    end
else
    % create a constant with a unique name
    NewName = generateName(obj,blockname,pConstList);
end

if IsNew
    % make a new cgconstant
    ptr = xregpointer(cgconstant(NewName,value));
    ptr.info = ptr.set('precision',cgprecfloat);
end        

function NewName = generateName(obj,blockname,pConstList)
NewName = obj.Project.uniquename(blockname);
pConst = pConstList( strcmp(NewName,pveceval(pConstList,@getname)));
Existing = ~isempty(pConst);
Count = 1;
while Existing
    NewName = obj.Project.uniquename(sprintf('%s_%d',blockname,Count));
    pConst = pConstList( strcmp(NewName,pveceval(pConstList,@getname)));
    Existing = ~isempty(pConst);
    Count = Count + 1;
end