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

    function [OK, msg, out] = xregReadDelimitedSweeps(filename, out,GuiMode) %#ok<INUSD>
%XREGREADDELIMITEDSWEEPS Read delimited sweep-based text files into MBC
%
%  [OK, MSG, OUT] = XREGREADDELIMITEDSWEEPS(FILENAME, OUT) reads delimited text
%  files into MBC.  These files can be delimited by tabs, |, commas or
%  spaces.  They may optionally contain a variable name header and units
%  header line.  The data can be split into multiple blocks where each
%  block contains data for a separate sweep.  Within each block, a column
%  that consists of a value in the first row and empty values in the
%  remaining rows for initial columns will be assumed as meaning that for
%  these column each row should take its value from the first row in the
%  block.

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


OK = 0;
msg = '';
if nargin<3
    GuiMode = true;
end

% Check that the file being requested exists
if exist(filename, 'file') ~= 2
    msg = 'The file does not exist.';
    return
end

[numVars, Names, Units, DelimChar, DelimAtEnd] = xregReadDelimInfo(filename);
if isempty(DelimChar)
    msg = 'Unable to determine the delimiter character.';
    return
end

numHeaders = 0;
if ~isempty(Names)
    out.varNames = Names;
    numHeaders = numHeaders + 1;
else
    out.varNames = repmat({'VAR'}, 1, numVars);
end
if ~isempty(Units)
    out.varUnits = Units;
    numHeaders = numHeaders + 1;
else
    out.varUnits = repmat({''}, 1, numVars);
end

if ~DelimAtEnd
    % Add the end of line as a delimiter
    DelimChar = [DelimChar '\n'];
end


fid = fopen(filename, 'rt');
for n = 1:numHeaders
    fgetl(fid);
end

if ~DelimAtEnd
    % Read in rest of file
    fileStr = fread(fid, 'uchar=>char').';
    
    % Replace multiple \n's with a single.  This fixes empty lines between
    % blocks
    fileStr = regexprep(fileStr, '\n+', '\n');

    TextStrings = textscan( fileStr,'%s','delimiter',DelimChar,'whitespace','\r\b' );Fields = TextStrings{1};
else
    % Empty lines won't cause a problem
    Fields = textscan(fid, '%s', -1, 'delimiter', DelimChar, 'whitespace', '\n\r\b');
    Fields = Fields{1};
end
fclose(fid);

numRows = fix(length(Fields)./numVars);
numOver = rem(length(Fields), numVars);
if numOver>0
    % There should never be trailing empty rows in this case as we have
    % already compressed them out with the regexprep call earlier on
    msg = 'Unable to determine the correct data array size.';
    return
end

data = xregcellstr2real(Fields);
data = reshape(data, numVars, numRows).';

% Remove rows that are all NaNs
data = data(~all(isnan(data),2),:);
numRows = size(data, 1);

% Replicate initial data above blocks of NaNs.  
if numRows>1
    for m = 1:numVars
        isn = isnan(data(:, m));
        if ~isn(2)
            break
        end
        for n = 2:numRows
            if isn(n)
                data(n,m) = data(n-1,m);
            end
        end
    end
end
out.data = data;
OK = 1;