www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@sweepset/insertRecords.m

    function [newSS, newRecordIndex] = insertRecords(SS, currentRecordIndex, newData)
%INSERTRECORDS Add records into pre existing sweeps 
%
%  [SS, NEWINDEX] = INSERTRECORDS(SS, INDEX, DATA)

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


% Copy the sweepset
newSS = SS;

% How many records are being added
recsToAdd = size(newData, 1);

% Make sure we have a sensible set of inputs
if ~isequal(length(currentRecordIndex), recsToAdd)
    error(message('mbc:sweepset:InvalidArgument7'));
elseif min(currentRecordIndex) < 1
    error(message('mbc:sweepset:InvalidIndex'));
elseif max(currentRecordIndex) > numRecords(SS)
    error(message('mbc:sweepset:InvalidIndex1'));
elseif ~isequal(numRecords(SS), size(newData, 2))
    error(message('mbc:sweepset:InvalidArgument8'));
end

% Make sure that currentRecordIndex is sorted correctly
[currentRecordIndex, index] = sort(currentRecordIndex);
newData = newData(index, :);

% Need to convert currentRecordIndex into a index in the new sweepset -
% i.e need to add one to each currentRecordIndex where there is a new
% record added before it. 
newRecordIndex = currentRecordIndex + cumsum(sign(currentRecordIndex));

% Generate the new data and baddata in correct size
newSS.data = zeros(numRecords(newSS), numDataVariables(newSS));
newSS.baddata = sparse(zeros(numRecords(newSS), numDataVariables(newSS)));

% Create a guid array large enought to reference into
newSS.guid = guidarray(numRecords(newSS));

% Create the logical reference into the data matrix
lOldData = true(1, numRecords(newSS));
lOldData(newRecordIndex) = 0;
lNewData = ~lOldData;

% Copy the new data
newSS.data(lNewData, :)     = newData;

% Copy the old data
newSS.data(lOldData, :)     = SS.data;
newSS.baddata(lOldData, :)  = SS.baddata;
newSS.guid(lOldData)        = SS.guid;

% Update the underlying xregdataset - need to work out where data was
% added
newSizes = tsizes(SS);
oldStartIndex = cumsum([1 newSizes]);
for i = 1:length(newSizes)
    numFound = sum(currentRecordIndex >= oldStartIndex(i) & currentRecordIndex < oldStartIndex(i+1));
    newSizes(i) = newSizes(i) + numFound;
end
newSS.xregdataset = xregdataset(testnum(SS), type(SS), newSizes);