|
"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
|