function [out, header] = readtable(infile) %READTABLE Read an ascii data file with a header line. % % USE: % [out, header] = readtable(infile) % % where infile is an existing file and % header is the header line of the data file. % The data file must be rectangular and missing values % should be specified with NaN's. % Tomo Eguchi % 7 March 2000 fid = fopen(infile); % Open the file if (fid <= 0), indexSlash = find(infile == '\'); pathname1 = infile(1:indexSlash(end)); fprintf(1, 'Select a file.'); [filename, pathname] = uigetfile([pathname1, '*.*'],'Open File: ',100,100); infile = [pathname,filename]; fid = fopen(infile); end fprintf(1,'\n') fprintf(1, 'Reading the file \n'); header = fgets(fid); % take the header line out out = []; % initialize the output vector c1 = 1; % initialize the counter while 1 line = fgets(fid); % take one line at a time if (line == -1), break, end % if no more line, end. data = str2num(line); % convert them into numbers if (isempty(data) ~= 1), % if line is empty, skip. out(c1,:) = data; % collect all data into one matrix. c1 = c1 + 1; % add one to the row index end end fclose(fid); % close the file.