www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@sweepsetfilter/private/updatevariable.m

    function [obj, ss] = updatevariable(obj, ss, startIndex)
%UPDATEVARIABLE Update the user-defined variables
%
%  [SSF, SS] = UPDATEVARIABLE(SSF, SS, INDEX) reapplies the user-defined
%  variables and returns the updated sweepset.  If an input sweepset is not
%  provided then one will be generated.

%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


f = getFlags;

% Have we got a current copy of the sweepset
if nargin < 2 || ~isa(ss, 'sweepset')
    ss = ApplyObject(obj, f.APPLY_DATA);
end

% Ensure that startIndex is initialised
if nargin < 3
    startIndex = 1;
end

% Store to see if variables are changed in the update process
INITIAL_variables = obj.variables;

% Reset variableSweepset field before iterating through variables
if startIndex == 1
    obj.variableSweepset = sweepset;
else
    ssEndIndex = startIndex - 1;
    % Need to ensure that all startIndex doesn't include some problem
    % variables. Find those variables which have errors
    for i = 1:ssEndIndex
        if ~obj.variables(i).OK
            ssEndIndex = ssEndIndex - 1;
        end
    end
    obj.variableSweepset = obj.variableSweepset(:, 1:ssEndIndex);
    % Need to append the unchanged variables to the current sweepset
    ss = [ss obj.variableSweepset];
end

% Iterate through all the stored variables
for i = startIndex:length(obj.variables)
    [varOK, value, obj.variables(i)] = i_evaluateVariable(obj.variables(i), ss);
    if varOK
        obj.variableSweepset = [obj.variableSweepset value];
        % need to augment sweepset iteratively so that variables can depend
        % on variables defined earlier
        ss = [ss value]; %#ok<AGROW>
    end
end


% Have the variables actually changed
if ~isequaln(obj.variables, INITIAL_variables)
    queueEvent(obj, 'ssfVariablesChanged');
end

% Now cascade the update to filters
[obj, ss] = updateFilter(obj, ss);


%--------------------------------------------------------------------------
function [varOK, value, variable] = i_evaluateVariable(variable, ss)

varOK = true;
value = [];
msg = '';

if ~isempty(find(ss, variable.varName))
    % Check that the new variable doesn't already exist in pSweepset or
    % variablesToAppend
    msg = 'Variable name already exists in sweepset.';
    varOK = false;
end

if varOK
    try
        % Evaluate the inline expression
        value = seval(ss, variable.inlineExp, variable.varName, variable.varUnit);
    catch ME
        varOK = false;
        [unused, unused, msg] = mbcGetLastError(ME,'MessageFormat', 'singleline');
    end
end

if varOK
    % Check size of values
    if size(value, 1) ~= size(ss, 1)
        varOK = false;
        msg = 'Size of calculated values must match input data size.';
    end
end

if varOK
    variable.result = 'Variable successfully added.';
else
    variable.result = sprintf('Error: %s', msg);
end
variable.OK = varOK;