function [mD] = big_textread (filename,dim,textcols,headlines)
% [mD] = big_textread (filename,dim,textcols,headlines)
%
% This function reads text files, of almost all sizes. No restrictions on the
% number of rows and cloumns as for as you know the number of cloumns in File.
% this fction is most usefull reading troublesome ASCII files.
% mD = data struct containing filed
% mD.labels Row headers
% mD.cnames Column headers
% mD.data Data
%
% Examples
% myData = big_textread ('myFile.txt',666,1,2)
% my file contains 666 columns, first column is text column describing row headers,
% First 2 rows are test rows describing Column headers
% Writen by Mujahid Sultan @ OCI, Princess Margret Hospital, Toronto.
% May, 2002
% m.sultan@utoronto.ca
%----------action
if textcols > 0
% read first column as labels
[l] = textread (filename,'%s%*[^\n]','delimiter','\t','headerlines',headlines);
mD.labels = l;
% prepare the format string
fstd = {''};
for i = 1:textcols
fstd = strcat({'%*s'},fstd);
end
else
mD.labels = '';
fstd = {''};
end
fstrd = strcat(fstd,{'%n%*[^\n]'});
mD.fstrd(1,:)= fstrd;
for i= 2:(dim-textcols)
fstrd = strcat({'%*s'}, fstrd);
mD.fstrd(i) = fstrd;
end
% initialize Load Wait bar
h1 = waitbar(0,'Loading TXT File...');
% get the cols data using format string
for i = 1:(dim-textcols)
[d] = textread (filename,[char(mD.fstrd(i))],'delimiter','\t','headerlines',headlines);
mD.data(i,:)= d;
% this is to show the load status bar
waitbar (i/(dim-textcols),h1);
end
close (h1)
% get cnames using format string with %s
fsts = {''};
for i = 1:textcols
fsts = strcat({'%*s'},fsts);
end
fstrs = strcat(fsts,{'%s%*[^\n]'});
mD.fstrs(1,:)= fstrs;
for i= 2:(dim-textcols)
fstrs = strcat({'%*s'},fstrs);
mD.fstrs(i) = fstrs;
end
for i = 1:(dim-textcols)
[s] = textread (filename,[char(mD.fstrs(i))],1,'delimiter','\t');
mD.cnames(i,:)= s;
end
clear mD.fstrs mD.fstrd
mD.data = mD.data';
aout = mD;