www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/cgcalreadtxt.m
function [data,headings,units,err] = cgcalreadtxt(file,delim) % Function to read .txt files % Copyright 2000-2011 The MathWorks, Inc. and Ford Global Technologies, Inc. headings = []; [~,~,e] = fileparts(file); if nargin<2 if strcmpi(e,'.csv') delim = ','; else delim = char(9); end end try fid = fopen(file); firstline = fgetl(fid); fclose(fid); if double(delim)==9 ws = '\b\r\n'; else ws = '\b\r\n\t'; end fid = fopen(file,'rt'); closeFile = onCleanup(@() fclose(fid)); data = textscan(fid,'%s','whitespace',ws,'delimiter',delim); data = data{1}; if firstline(end)~=delim && ~strcmp(delim,' ') firstline = [firstline delim]; end spaces = find(double(firstline)==double(delim)); spaces = spaces([1 find(diff(spaces)~=1)+1]); nsp = length(spaces); count = 1; namesflag = 0; units = []; varnames = {[]}; start = 1; if isempty(varnames{1}) for i = 1:nsp varnames{i} = ['Var',num2str(i)]; end end if isempty(units) for i = 1:length(varnames) units{i} = 'Number'; end end if start>length(data) || (start-1+nsp)>length(data) data = []; else for i = 1 : (length(data)+1-start)/nsp newd = data(start+nsp*(i-1):start-1+nsp*i); if strcmp(newd{1,1},varnames{1}) namesflag = 1; elseif namesflag == 1 namesflag = 0; else d(count,:) = newd; count = count+1; end end for i = 1 : length(varnames) if size(d,1)>0 && size(d,2)>=i && isempty(str2num(d{1,i})) numind(i) = 0; else if size(d,1)>1 && size(d,2)>=i && isempty(str2num(d{2,i})) for j = 1:size(d,1) if ~isempty(d{j,i}) curval = d{j,i}; else d{j,i} = curval; end end end numind(i) = 1; end end numind = (numind~=0); d = d(:,numind); data = str2double(d); end catch data = []; headings = []; units = []; err = 'Data could not be read. Check format'; end