Path: news.mathworks.com!not-for-mail
From: "Miroslav Balda" <miroslav.nospam@balda.cz>
Newsgroups: comp.soft-sys.matlab
Subject: Re: txt2mat
Date: Mon, 10 Nov 2008 21:43:02 +0000 (UTC)
Organization: Miroslav Balda
Lines: 89
Message-ID: <gfa9t6$dig$1@fred.mathworks.com>
References: <gf9ogb$mrb$1@fred.mathworks.com>
Reply-To: "Miroslav Balda" <miroslav.nospam@balda.cz>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226353382 13904 172.30.248.35 (10 Nov 2008 21:43:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Nov 2008 21:43:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 360709
Xref: news.mathworks.com comp.soft-sys.matlab:500088


"Zahra" <zahra.yamani@nrc.gc.ca> wrote in message <gf9ogb$mrb$1@fred.mathworks.com>...
> Hi all;
> 
> I have an ascii file that has a combination of text and data. The text is a header that repeats throughout the file. The data is of the follwoing format where the line is folded for the same data point:
> 
> Point     v1          v2           v3            v4  
> Point     v5          v6 
>   1        -73         -112        15000        61  
>   1         79          79                             
>   2        -74        -112         25000        61  
>   2         72          74  
> and so on...
> 
> When I use txt2mat, what I get is the follwoing:  
> 
> [1 v1 v2 v3 v4]
> [1 v5 v6 NaN NaN]
> [2 v1 v2 v3 v4]
> [2 v5 v6 NaN NaN]
> 
> What I want instead is a matrix where all the values for the same data point are saved in one row as follows: i.e. avoid the line folding in the final matrix:
> 
> [1 v1 v2 v3 v4 v5 v6]
> [2 v1 v2 v3 v4 v5 v6]
> 
> 
> Any advice as how this can be done is appreciated.
> 
> Thanks,
> Zahra

Hi Zahra,

You may try also the function for free format read -  ffread, 
FEX Id 9034 by calling:

The data file was 'data.txt':
Point     v1          v2           v3            v4  
Point     v5          v6 
  1        -73         -112        15000        61  
  1         79          79                             
  2        -74        -112         25000        61  
  2         72          74  
Point     v1          v2           v3            v4  
Point     v5          v6 
  1        -73         -112        15000        61  
  1         79          79                             
  2        -74        -112         25000        61  
  2         72          74  
Point     v1          v2           v3            v4  
Point     v5          v6 
  1        -73         -112        15000        61  
  1         79          79                             
  2        -74        -112         25000        61  
  2         72          74  

The script for reading it is 

ffread('data.txt'); %   Initiate Free format reading
first = ffread;        %   First item of header
A = zeros(100,7);   %   Estimated size of a matrix A
k = 0;
while first ~= 127  %    the end of data?
    ffread(7);           %   rest of headers
    for j = 1:2
        k = k+1;
        A(k,1:5) = ffread(5);   
        ffread;            %   skip second ident. of point
        A(k,6:7) = ffread(2);
    end
    first = ffread;
end
A = A(1:k,:)

with resulting matrix:

A =
           1         -73        -112       15000          61          79          79
           2         -74        -112       25000          61          72          74
           1         -73        -112       15000          61          79          79
           2         -74        -112       25000          61          72          74
           1         -73        -112       15000          61          79          79
           2         -74        -112       25000          61          72          74

It's all.

Mira