From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: read numerical text file by fixed column width, no delimiters
Date: Tue, 18 Jun 2013 13:04:38 -0500
Organization: NNTP Server
Lines: 36
Message-ID: <kpq7fb$i95$>
References: <kpo59r$4ra$>
NNTP-Posting-Host: 9tOBKKK+lO/
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20120306 Thunderbird/3.1.20
X-Notice: Filtered by postfilter v. 0.8.2
Xref: comp.soft-sys.matlab:797780

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.