[Index for tmp_for_tar/imgio] [Return to Master Index]

readnums

(tmp_for_tar/imgio/readnums.m)


Function Synopsis

[nn, ...] = readnums (fid, n)

Help text

 m = readnums (fid, n)       - Read n ascii integers from fid
 [m1,...,mn] = readnumns (fid,n)

 Comments -anything between a "#" and a "\n"- are ignored. Anything other
 than digits in "0"-"9" is ignored. Each number is defined by one or more
 consecutive digits. This function is slow.

 INPUT ---------
 fid   : 1 x 1 : File identifier.
 n     : 1 x 1 : Number of integers to be read.

 OUTPUT --------
 readum returns either
 - A single row vector containing all the integers it managed to read (the
   length may be less than n). Or,
 - Any number of integers it managed to read.

 Last modified: Setembro 2002



Cross-Reference Information

This function calls

Listing of function file tmp_for_tar/imgio/readnums.m

## m = readnums (fid, n)       - Read n ascii integers from fid
## [m1,...,mn] = readnumns (fid,n)
##
## Comments -anything between a "#" and a "\n"- are ignored. Anything other
## than digits in "0"-"9" is ignored. Each number is defined by one or more
## consecutive digits. This function is slow.
##
## INPUT ---------
## fid   : 1 x 1 : File identifier.
## n     : 1 x 1 : Number of integers to be read.
##
## OUTPUT --------
## readum returns either
## - A single row vector containing all the integers it managed to read (the
##   length may be less than n). Or,
## - Any number of integers it managed to read.

## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: Setembro 2002

function [nn, ...] = readnums (fid, n)

nn = zeros (1,n);

if n == 0, return; end

nread = 0;
curnum = [];

while n > nread

  [c,cnt] = fread (fid,1,"char");
  if !cnt, break; end

  if ! length (curnum)		# If not within an integer

    if   c == toascii ("#")	# Am I at beginning of comment?

      advance_until (fid,"\n"); # Yes. Skip it.

				# Am I at the beginning of integer?
    elseif c >= toascii("0") && c <= toascii("9")
      curnum = [curnum,c];	# Yes. Start reading it.
    end

  else
				# Am I still within the current integer?
    if c >= toascii("0") && c <= toascii("9")
      curnum = [curnum,c];	# Yes. Append digit.

    else			# No, save the integer I just read.
      nn(++nread) = str2num (setstr (curnum));
      curnum = [];
				# Eventually skip comment.
      if c == toascii ("#"), advance_until (fid, "\n"); end

    end
  end
end


## fseek (fid, ftell (fid) - 1);
if nread < n, nn = nn(1:nread); end

if nargout > 1
  for i = 2:nread, vr_val (nn(i)); end
  nn = nn(1);
end  

Produced by oct2html on Sat Sep 14 9:47:03 2002
Cross-Directory links are: ON