www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoppoint/cg_read_txt.m

    function [data,headings,units,err] = cg_read_txt(op,path,file,delim)
%CG_READ_TXT  Read text from delimited text files or clipboard
%
%  [DATA, NAMES, UNITS, ERR] = CG_READ_TXT(OP, PATH, FILE) reads in the
%  specified file and returns its data.  If PATH is 'clipboard' the text
%  will be taken from the clipboard.

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


err = '';
data = [];
headings = {};
units = {};

s = struct('varNames',{{}},'varUnits',{{}},'data',[]);
if strcmp(path,'clipboard')
    [ok, err, s] = xregReadClipboard([],s);
    
else
    [ok, err, s] = xregReadDelimited(fullfile(path, file),s);
    
end
data = s.data;
headings = s.varNames;
units = s.varUnits;

if ~isempty(headings)
    % Check for the fist column being NaNs: happens when data is an excel sheet
    % with Names:, Units:, Data: sections
    if all(isnan(data(:,1))) && strcmpi(headings{1}, 'Name:') || strcmpi(headings{1}, 'Names:')
        data = data(:,2:end);
        headings = headings(2:end);
        units = units(2:end);
    end

    % Check names and units and use dataset defaults for the missing ones
    VarIndex = 1;
    for n = 1:length(headings)
        if strcmp(headings{n}, 'VAR')
            headings{n} = sprintf('Var%s', VarIndex);
            VarIndex = VarIndex+1;
        end
    end
    for n = 1:length(units)
        if isempty(units{n})
            units{n} = 'Number';
        end
    end
end