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

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

%  Copyright 2000-2008 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, f.APPLY_VARS]);
end

% Ensure that index is initialised
if nargin < 3
    index = 1:length(obj.filters);
end

% Store to see if filters are changed in the update process
INITIAL_filters = obj.filters;

% Logical vector indicating which records to remove, none to start with
lRecordsToRemove = false(size(ss, 1), 1);

% Iterate through the filters
for i = 1:length(obj.filters)
    % Do we need to re-evaluate this filter?
    if ismember(i, index)
        % Evaluate this filter
        obj.filters(i) = i_evaluateFilter(obj.filters(i), ss);
    end
    if ~isempty(lRecordsToRemove)
        % OR the filterResult with the previous removed records
        lRecordsToRemove = lRecordsToRemove | obj.filters(i).filterResult;
    end
end

% Which guids have been removed?
lRecordsToRemove = lRecordsToRemove | ismember(getGuids(ss), obj.filterGuid);

% Record oldRecordsToRemove to see if we think we should fire ssfBadDataChanged
oldRecordsToRemove = obj.recordsToRemove;

% Which records have been removed
obj.recordsToRemove = find(lRecordsToRemove);

% Have the filters actually changed
if ~isequaln(obj.filters, INITIAL_filters)
    queueEvent(obj, 'ssfFiltersChanged');
end

% Should fire ssfBadDataChanged
if ~isequal(obj.recordsToRemove, oldRecordsToRemove)
    queueEvent(obj, 'ssfBadDataChanged');
end

% Update the current copy of the sweepset
ss = ApplyObject(obj, f.APPLY_FILT, ss);

% Now cascade the update to the tests
[obj, ss] = updateDefineTests(obj, ss);


%--------------------------------------------------------------------------
function filter = i_evaluateFilter(filter, ss)

filtOK = true;
msg = '';

try
    % Evaluate the filter criteria using sweepset/seval, true to keep data
    lKeepRecords = seval(ss, filter.inlineExp);
catch
    filtOK = false;
    msg = 'Invalid variable name or expression';
end

if filtOK && numel(lKeepRecords) ~= size(ss, 1)
    filtOK = false;
    msg = 'Returned array is incorrectly sized';
end

if filtOK
    % Invert for removed records
    lRemoveRecords = logical(~lKeepRecords);
    
    % Store the logical removed array efficiently
    filter.filterResult = mbcCompressLogicalArray(lRemoveRecords);
    
    % How many have been removed by this filter
    filter.result = sprintf('Filter successfully applied : %d records excluded.', sum(lRemoveRecords));
else
    filter.filterResult = sparse(false(size(ss, 1), 1));
    filter.result = sprintf('Error : Ignoring filter definition ''%s'': %s.', ...
        filter.filterExp, msg);
end
filter.OK = filtOK;