Path: news.mathworks.com!not-for-mail
From: "Kristin " <phillips@gps.caltech.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: read numerical text file by fixed column width, no delimiters
Date: Wed, 19 Jun 2013 19:36:11 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 41
Message-ID: <kpt17b$ie3$1@newscl01ah.mathworks.com>
References: <kpo59r$4ra$1@newscl01ah.mathworks.com> <kpq7fb$i95$1@speranza.aioe.org>
Reply-To: "Kristin " <phillips@gps.caltech.edu>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1371670571 18883 172.30.248.45 (19 Jun 2013 19:36:11 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 19 Jun 2013 19:36:11 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1176411
Xref: news.mathworks.com comp.soft-sys.matlab:797832

dpb <none@non.net> wrote in message <kpq7fb$i95$1@speranza.aioe.org>...
> On 6/17/2013 6:15 PM, Kristin wrote:
> > I have a large text file containing 8 columns of numbers and each column
> > has a fixed width of 10 characters (so the total length of each row is
> > 80 characters). The first character in each group of 10 characters is
> > either a space or a negative sign.
> > So for example a typical line is:
> > -1.6999E-3-1.3266E-3 1.2916E-3 3.5491E-4 1.1022E-3-9.5555E-4-3.9528E-4
> > 3.5200E-4
> > I was trying various approaches with textread or textscan and was trying
> > to use %c because %s requires whitespace separated values which is not
> > always the case here. Is there an easy way to read such a file in
> > Matlab? I either need eight vectors of numbers (ideally)...
> 
> Oh, forgot another "trick" I've used on occasion.  How well it works 
> depends on the size of the file...if can hold in memory works pretty 
> good; if have to can do in "chunks" that do fit...
> 
> Given you have a line as yours above I make it two to demonstrate for 
> the general case...
> 
>  >> m=[l;l];
>  >> v=reshape(str2num(reshape(m',10,[])'),8,[])'
> v =
> -0.0017  -0.0013   0.0013   0.0004   0.0011  -0.0010  -0.0004   0.0004
> -0.0017  -0.0013   0.0013   0.0004   0.0011  -0.0010  -0.0004   0.0004
>  >>
> 
> Just have to know the number per line and the width of the field for the 
> two reshape() operations.  The first casts the input character array 
> into a column of the right number of characters/line one field/row; the 
> second recasts into the known number of entries/line.
> 
> Again and still a lot of machinations for what _should_ be doable 
> directly by the i/o runtime formatting but C is "just broke" in this regard.
> 
> --

Thanks for the suggestions.  I will try one of those methods (reshape or fgetl to do it a line at a time).  Otherwise I'll see if I can just make a quick fortran script although I haven't previously used fortran for my coding.

Kristin