From octave-sources-request at bevo dot che dot wisc dot edu Mon Nov 8 08:07:57 1999 Subject: Reading ascii data file with comments From: Vladimir Eltsov To: octave-sources at bevo dot che dot wisc dot edu Date: Mon, 08 Nov 1999 16:07:54 +0200 This is a multi-part message in MIME format. --------------5C8B22527A624BDEB6962C51 Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: 7bit Hello, Here is octave m-file which reads data from ascii file. Unlike built-in 'load' function it allows to ignore arbitrary comments or to collect them in a string output argument. Also it can distribute data columns to different arrays for you. This program uses no octave functionality beyond 2.0.x, but unfortunately it doesn't work there due to octave bugs. So version later than 2.1.20 is required. Regards, Vladimir Eltsov --------------5C8B22527A624BDEB6962C51 Content-Type: text/plain; charset=koi8-r; name="read_ascii_data.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="read_ascii_data.m" function [...] = read_ascii_data (file, incols, comchar) # usage: x = read_ascii_data (file, ncols) # [x, comstr] = read_ascii_data (file, ncols, comchar) # [x_1, x_2, ..., x_ncols] = read_ascii_data (file) # [x_1, x_2, ..., x_ncols, comstr] = read_ascii_data (file, comchar) # # This function read the table with 'ncols' columns from # ascii file. Either string for a file name or file id is passed # through 'file' parameter. If file name is "-" then # stdin is used. Result is placed to matrix 'x' # or each column is placed to x_i. Non-parseable input is # ignored. Those lines which starts with 'comchar' char # are collected in 'comstr' output argument. ### Note: this function is usable only with octave versions > 2.1.20 # Vladimir Eltsov # veltsov at mail dot ru # 8 November 1999 switch (nargin) case (1) ncols = nargout; cm = 0; cc = 0; case (2) if (isstr(incols)) ncols = nargout-1; cm = 0; cs = incols(1); cc = 1; else ncols = incols; cm = 1; cc = 0; endif case (3) ncols = incols; cm = 1; cs = comchar(1); cc = 1; otherwise usage("read_ascii_data(file [,ncols] [,comment_char])"); endswitch ncols = max(ncols,1); # if result goes to ans, then nargout==0 do_close = 0; if (isstr(file)) if (strcmp(file,"-")) fin = stdin; else fin = fopen(file,"r"); if (fin<0) error(["read_ascii_data: cannot open input file ", file]); endif do_close = 1; endif else if (! is_scalar(file) && ! is_stream(file)) error("read_ascii_data: 'file' parameter should be string or file id"); endif fin = file; endif y = []; cb = ""; save_eleo = empty_list_elements_ok; unwind_protect empty_list_elements_ok = 1; while (! feof(fin)) [z, k] = fscanf(fin,"%f",Inf); if (k>0) y = [y; z]; endif s = fgets(fin); if (isstr(s) && cc && s(1)==cs) cb = [cb s]; endif endwhile unwind_protect_cleanup empty_list_elements_ok = save_eleo; end_unwind_protect if (do_close) fclose(fin); endif k = length(y); c = rem(k,ncols); if (c != 0) warning("read_ascii_data: non-exact number of rows were read"); y = [y; zeros(ncols-c,1)]; endif x = reshape(y,ncols,length(y)/ncols)'; if (cm) vr_val(x); else for i=1:ncols vr_val(x(:,i)) endfor endif if (cc) vr_val(cb); endif endfunction --------------5C8B22527A624BDEB6962C51--