[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reading ASCII data of unknown length
From: |
Vinayak Dutt |
Subject: |
Re: Reading ASCII data of unknown length |
Date: |
Fri, 26 May 95 08:10:39 CDT |
Hi:
The major problem is reading text files is not knowing the
size of file (ie, number of entries). The way I solve the
problem is by executing a shell call to Unix function "wc"
(word count program for text files). "wc" will return the number
of words (also number of lines or number of characters if one
desires). So the way I implement my text file reading .m function
is like:
function x = readf (name)
#
# Usage: x = readf (name)
#
# reads the text file contents as a matrix of real values of one
# columns
#
if (nargin != 1)
error("usage: readf (filename)");
endif;
#
# get the number of data points
#
# this is done via Unix command wc (word count) which returns only the
# number of words (ie, number of data values in the file) with option -w
#
command = sprintf("wc -w %s",name);
size = sscanf(system(command,1),"%d");
#
file = fopen(name,"r");
if (file == 0)
error("readf: unable to open the file");
endif;
#
for i = 1:size
x(i,1) = fscanf(file,"%f");
end;
#
fclose(file);
endfunction