Thread Subject: txt2mat

Subject: txt2mat

From: Zahra

Date: 10 Nov, 2008 16:46:03

Message: 1 of 4

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

Subject: txt2mat

From: Miroslav Balda

Date: 10 Nov, 2008 21:43:02

Message: 2 of 4

"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

Subject: txt2mat

From: Zahra

Date: 10 Nov, 2008 23:54:01

Message: 3 of 4

Hi Mira,

Thanks for your reply.

The code you sent workd if there is no other headerlines in the file. In my case, the actual data files have teh follwoing format:

-----------------------------------
header1

Point v1 v2 v3 v4
Point v5 v6
  1 -73 -112 15000 61
  1 79 79
  2 -74 -112 25000 61
  2 72 74




header2

Point v1 v2 v3 v4
Point v5 v6
  1 -73 -112 15000 61
  1 79 79
  2 -74 -112 25000 61
  2 72 74




header3

Point v1 v2 v3 v4
Point v5 v6
  1 -73 -112 15000 61
  1 79 79
  2 -74 -112 25000 61
  2 72 74

----------------------------------------------

With this format, when I try your code, I get the follwoing error:

??? Conversion to cell from double is not possible.

Error in ==> ffread at 142
            data(k) = sscanf(st_(nc_:J_(j_)-1),'%e');

Any further advice is greatly appreciated.

Thanks,
Zahra

Subject: txt2mat

From: Andres

Date: 11 Nov, 2008 08:37:02

Message: 4 of 4

Hi there,
I think we've got two parallel threads here. I've posted my reply to http://www.mathworks.com/matlabcentral/newsreader/view_thread/239003
.
Regards
Andres

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
ffread Miroslav Balda 10 Nov, 2008 16:45:06
txt2mat Zahra 10 Nov, 2008 12:23:50
line folding Zahra 10 Nov, 2008 12:19:43
ascii data import Zahra 10 Nov, 2008 12:19:43
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com