help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reading ASCII data of unknown length


From: Francesco Potorti`
Subject: Reading ASCII data of unknown length
Date: Fri, 26 May 1995 13:56 +0100 (MET)

      I need to read data (ASCII floating point numbers) from a file
      specified by the user.  The trouble is that I don't know the
      length of the input file, so I have to fscanf(...) until EOF,
      which generates an error message which may alarm users.

   Well this is something I wrote...it may do it for you...or give you
   a start.  It's really slow for large files....
   [...]

I use the appended shell script. It takes a file, it counts the number
of words on the first line, assumes that is the number of columns,
than it counts the number of lines, assuming that is the number of
rows, and adds a header to the file in order to make it immediately
readable by octave with the `load' command.  It's really fast :)

#
# mkmatrix              Fri May 26 13:53:05 MET 1995   address@hidden
#
# This shell script takes a text file in tabular form (a matrix of real
# numbers) and adds a header to it in order to make it readable by octave.
#
# The syntax is:
#    mkmatrix file [varname]
# where file is the file to be edited, and varname is the variable name
# that will be put in the header.  If varname is omitted, it defaults to
# file.
#
# The script runs under sh and ksh, on systems that have `basename'.  If
# you do not have `basename', you can as well delete it (or substitute
# every occurrence of `basename' with `echo').  It should work as long
# as file is in the current directory.
#

if [ $# != 1 -a $# != 2 ]
then    echo usage `basename $0` file \[varname\]
        exit 2
fi

file=$1
var=${2:-`basename $file`}

set - `wc -l $file`
rows=$1
read line < $file
set - `echo "$line" | wc -w`
cols=$1

set -e          # exit on any error

tmpfile=octhead.$$
echo "# name: $var" > $tmpfile
echo "# type: matrix" >> $tmpfile
echo "# rows: $rows" >> $tmpfile
echo "# columns: $cols" >> $tmpfile
cat $file >> $tmpfile
mv $tmpfile $file


reply via email to

[Prev in Thread] Current Thread [Next in Thread]