[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reading ASCII data of unknown length
From: |
niles |
Subject: |
Re: Reading ASCII data of unknown length |
Date: |
Thu, 25 May 95 11:19:05 -0400 |
>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....
-------------------
jwe: Request: Please add reading of plain ASCII files as a single matrix w/
- white space (space, tab) delimited columns
- CR or NL delimited rows.
- use the filename as the matrix name.
------------------
Good Luck,
Rick Niles.
----------------
% Function to read in time history file without titles.
% function data = loadth(title, header, ncols, nrows)
% by Rick Niles <address@hidden>
%
function data = loadth(title, header, ncols, nrows)
fp = fopen(title, "r");
fseek(fp, 0, SEEK_END);
eof = ftell(fp);
frewind(fp);
if (header)
headeris =1
for col=1:ncols
name = fscanf(fp, "%s")
endfor
endif
if (~exist('nrows'))
eoh = ftell(fp);
pos = 0;
nrows = 1;
while( pos < eof-4 )
for col=1:ncols
a = fscanf(fp, "%lf");
endfor
nrows++;
pos = ftell(fp);
endwhile
nrows
fseek(fp, eoh, SEEK_SET);
endif
data = zeros(nrows,ncols);
pos = 0;
row = 1;
for row=1:nrows
for col=1:ncols
data(row,col) = fscanf(fp, "%lf");
endfor
endfor
endfunction