[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reading tab-delimited ASCII files
From: |
Bob Weigel |
Subject: |
Re: Reading tab-delimited ASCII files |
Date: |
Wed, 08 Aug 2007 16:10:41 -0400 |
User-agent: |
KMail/1.8.2 |
On Tuesday 07 August 2007 15:36, Matthias Brennwald wrote:
> Dear all
>
> I've got some ASCII file containing data which I'd like to read into
> Octave. The ASCII files contain several lines and columns. The columns
> are separated by tabs. The first column contains strings or variable
> width (parameter names), the remaining columns contain mostly numbers
> and some NaN values (e.g. 'N/A'). As an example, a file might look like
> this:
>
> K_G 1E-2 N/A 0.1
> KD_FIN 2 1.9 1.8
> KD_COA 0.2 0.02 1.2E-2
> T_LI 9.3244 8.233 N/A
>
> I tried using fscanf to read the data from the files, but I don't
> understand how to use the template parameter to specify the data format.
> I ended up reading line by line with fgetl and then analyzing each line
> seperately. This works, but it's slow. I believe that the fscanf
> function would be much faster. Can anyone give me an example of how to
> efficiently read ASCII files, e.g. the one above?
>
> Matthias
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
id = fopen('file.txt','rt');
s = fscanf(id,'%c');
fclose(id);
s = regexprep(s,'N\/A','NaN')
s = regexprep(s,'K_G','0')
s = regexprep(s,'KD_FIN','1')
s = regexprep(s,'KD_COA','2')
s = regexprep(s,'T_LI','3')
m = str2num(s)