loadtxt() - load ascii text file into numeric or cell arrays Usage: >> array = loadtxt( filename, 'key', 'val' ...); Inputs: filename - name of the input file Optional inputs 'skipline' - number of lines to skip {default:0}. If this number is negative the program will only skip non-empty lines (can be usefull for files transmitted from one platform to an other, as CR may be inserted at every lines). 'convert' - 'on' standard text conversion, see note 1 'off' no conversion, considers text only 'force' force conversion, NaN are returned for non-numeric inputs {default:'on'} 'delim' - ascii character for delimiters. {default:[9 32] i.e space and tab}. It is also possible to enter strings, Ex: [9 ' ' ',']. 'blankcell' - ['on'|'off'] extract blank cells {default:'on'} 'verbose' - ['on'|'off'] {default:'on'} 'nlines' - [integer] number of lines to read {default: all file} Outputs: array - cell array. If the option 'force' is given, the function retrun a numeric array. Notes: 1) Since it uses cell arrays, the function can handle text input. The function reads each token and then try to convert it to a number. If the conversion is unsucessfull, the string itself is included in the array. 2) The function adds empty entries for rows that contains fewer columns than others. Author: Arnaud Delorme, CNL / Salk Institute, 29 March 2002
0001 % loadtxt() - load ascii text file into numeric or cell arrays 0002 % 0003 % Usage: 0004 % >> array = loadtxt( filename, 'key', 'val' ...); 0005 % 0006 % Inputs: 0007 % filename - name of the input file 0008 % 0009 % Optional inputs 0010 % 'skipline' - number of lines to skip {default:0}. If this number is 0011 % negative the program will only skip non-empty lines 0012 % (can be usefull for files transmitted from one platform 0013 % to an other, as CR may be inserted at every lines). 0014 % 'convert' - 'on' standard text conversion, see note 1 0015 % 'off' no conversion, considers text only 0016 % 'force' force conversion, NaN are returned 0017 % for non-numeric inputs {default:'on'} 0018 % 'delim' - ascii character for delimiters. {default:[9 32] 0019 % i.e space and tab}. It is also possible to enter 0020 % strings, Ex: [9 ' ' ',']. 0021 % 'blankcell' - ['on'|'off'] extract blank cells {default:'on'} 0022 % 'verbose' - ['on'|'off'] {default:'on'} 0023 % 'nlines' - [integer] number of lines to read {default: all file} 0024 % 0025 % Outputs: 0026 % array - cell array. If the option 'force' is given, the function 0027 % retrun a numeric array. 0028 % 0029 % Notes: 1) Since it uses cell arrays, the function can handle text input. 0030 % The function reads each token and then try to convert it to a 0031 % number. If the conversion is unsucessfull, the string itself 0032 % is included in the array. 0033 % 2) The function adds empty entries for rows that contains 0034 % fewer columns than others. 0035 % 0036 % Author: Arnaud Delorme, CNL / Salk Institute, 29 March 2002 0037 0038 % Copyright (C) Arnaud Delorme, CNL / Salk Institute, 29 March 2002 0039 % 0040 % This program is free software; you can redistribute it and/or modify 0041 % it under the terms of the GNU General Public License as published by 0042 % the Free Software Foundation; either version 2 of the License, or 0043 % (at your option) any later version. 0044 % 0045 % This program is distributed in the hope that it will be useful, 0046 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0047 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0048 % GNU General Public License for more details. 0049 % 0050 % You should have received a copy of the GNU General Public License 0051 % along with this program; if not, write to the Free Software 0052 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 0053 0054 function array = loadtxt( filename, varargin ); 0055 0056 if nargin < 1 0057 help loadtxt; 0058 return; 0059 end; 0060 if ~isempty(varargin) 0061 try, g = struct(varargin{:}); 0062 catch, disp('Wrong syntax in function arguments'); return; end; 0063 else 0064 g = []; 0065 end; 0066 0067 g = finputcheck( varargin, { 'convert' 'string' { 'on';'off';'force' } 'on'; 0068 'skipline' 'integer' [0 Inf] 0; 0069 'verbose' 'string' { 'on';'off' } 'on'; 0070 'uniformdelim' 'string' { 'on';'off' } 'off'; 0071 'blankcell' 'string' { 'on';'off' } 'on'; 0072 'delim' { 'integer';'string' } [] [9 32]; 0073 'nlines' 'integer' [] Inf }); 0074 if isstr(g), error(g); end; 0075 if strcmpi(g.blankcell, 'off'), g.uniformdelim = 'on'; end; 0076 g.convert = lower(g.convert); 0077 g.verbose = lower(g.verbose); 0078 g.delim = char(g.delim); 0079 0080 % open the file 0081 % ------------- 0082 if exist(filename) ~=2, error( ['file ' filename ' not found'] ); end; 0083 fid=fopen(filename,'r','ieee-le'); 0084 if fid<0, error( ['file ' filename ' found but error while opening file'] ); end; 0085 0086 index = 0; 0087 while index < abs(g.skipline) 0088 tmpline = fgetl(fid); 0089 if g.skipline > 0 | ~isempty(tmpline) 0090 index = index + 1; 0091 end; 0092 end; % skip lines --------- 0093 0094 inputline = fgetl(fid); 0095 linenb = 1; 0096 if strcmp(g.verbose, 'on'), fprintf('Reading file (lines): '); end; 0097 while isempty(inputline) | inputline~=-1 0098 colnb = 1; 0099 if ~isempty(inputline) 0100 tabFirstpos = 1; 0101 0102 % convert all delimiter to the first one 0103 if strcmpi(g.uniformdelim, 'on') 0104 for index = 2:length(g.delim) 0105 inputline(find(inputline == g.delim(index))) = g.delim(1); 0106 end; 0107 end; 0108 0109 while ~isempty(deblank(inputline)) 0110 if strcmpi(g.blankcell,'off'), inputline = strtrim(inputline); end; 0111 if tabFirstpos && length(inputline) > 1 && all(inputline(1) ~= g.delim), tabFirstpos = 0; end; 0112 [tmp inputline tabFirstpos] = mystrtok(inputline, g.delim, tabFirstpos); 0113 switch g.convert 0114 case 'off', array{linenb, colnb} = tmp; 0115 case 'on', 0116 tmp2 = str2double(tmp); 0117 if isnan( tmp2 ) , array{linenb, colnb} = tmp; 0118 else array{linenb, colnb} = tmp2; 0119 end; 0120 case 'force', array{linenb, colnb} = str2double(tmp); 0121 end; 0122 colnb = colnb+1; 0123 end; 0124 linenb = linenb +1; 0125 end; 0126 inputline = fgetl(fid); 0127 if linenb > g.nlines 0128 inputline = -1; 0129 end; 0130 if ~mod(linenb,10) & strcmp(g.verbose, 'on'), fprintf('%d ', linenb); end; 0131 end; 0132 if strcmp(g.verbose, 'on'), fprintf('%d\n', linenb-1); end; 0133 if strcmp(g.convert, 'force'), array = [ array{:} ]; end; 0134 fclose(fid); 0135 0136 % problem strtok do not consider tabulation 0137 % ----------------------------------------- 0138 function [str, strout, tabFirstpos] = mystrtok(strin, delim, tabFirstpos); 0139 % remove extra spaces at the beginning 0140 while any(strin(1) == delim) && strin(1) ~= 9 && strin(1) ~= ',' 0141 strin = strin(2:end); 0142 end; 0143 % for tab and coma, consider empty cells 0144 if length(strin) > 1 && any(strin(1) == delim) 0145 if tabFirstpos || any(strin(2) == delim) 0146 str = ''; 0147 strout = strin(2:end); 0148 if strin(2) ~= 9 && strin(2) ~= ',' 0149 tabFirstpos = 0; 0150 strout = strtrim(strout); 0151 end; 0152 else 0153 [str, strout] = strtok(strin, delim); 0154 end; 0155 else 0156 [str, strout] = strtok(strin, delim); 0157 end;