Read a complex text file and then rearrange it

4 次查看(过去 30 天)
Hello people!
I have a .txt file I need to import in MATLAB, read and then rearrange. But let's stick to the reading part for now. The text file may be found here:
As you may notice the file structure is several lines of header, a four column matrix, some lines of header again and then a five column matrix until the end of the file.
What do I need to keep: 1. some numbers from both header groups 2. 2 first columns from the first matrix 3. all columns from the last matrix
My inquiry is: 1. How can I read the first four headers and store them (numbers as numbers and text as text)? 2. How can I read the first two columns of the first matrix and ignore the others. Note that 48 is the length of number of rows of the first matrix. Is it possible to keep the first two columns in two separate single column matrices A(n,1)? 3. Read the next set of three headers like in point 1 4. How can I real all the columns of the last matrix and store the data in five single column matrices? Note that 680 is the number of rows of the last matrix.
I have some knowledge of FORTRAN but it's been too long since I used it, plus I don't own a FORTRAN license anymore. MATLAB is my only option. So I would appreciate it if a brief desription followed each code line.
Thank you in advance!
Giorgos

采纳的回答

Thorsten
Thorsten 2013-1-15
fid = fopen('data.txt');
s = fgets(fid); % get first line
num1 = sscanf(s, '%d') % convert to number
s = fgets(fid); % get next, i.e, second line
s = fgets(fid); % get next, i.e, third line
num2 = sscanf(s, '%d')
s = fgets(fid); % get next, i.e, fourth line
Nlines = sscanf(s, '%d')
M1 = fscanf(fid, '%f\n', 4*Nlines);
M1 = reshape(M1, 4, [])';
% keep the first two columns in two separate single column matrices
C11 = M1(:, 1);
C12 = M1(:, 2);
header2 = fscanf(fid, '%f\n', 3);
Nlines = header2(2)
M2 = fscanf(fid, '%f\n', 5*Nlines);
M2 = reshape(M2, 5, [])';
% store the data in five single column matrices
C21 = M2(:, 1);
C22 = M2(:, 2);
C23 = M2(:, 3);
C24 = M2(:, 4);
C25 = M2(:, 5);
fclose(fid)

更多回答(1 个)

Giorgos Tassis
Giorgos Tassis 2013-1-16
Thank you so much Thorsten!
One additional question if you will please. Let's say there was no text in the headers and I would like to read them with a for loop. The structure would be something like
for i = 1:4 s(i)=fgets(fid); num(i)=sscanf(s(i),'%d'); end
I know this is something between FORTRAN and MATLAB, but I would be grateful if you could clarify how to create matrices with a for loop.
  1 个评论
Thorsten
Thorsten 2013-1-16
One nice think about Matlab is that many functions are vectorized so you wouldn't need for loop as often as in other languages.
Your example works if every line contains a single whole number (%d). If there are more numbers sscanf will return all numbers, and you have to assign them to num(i, :), i.e, the i'th row in matrix num:
for i = 1:4
s(i) = fgets(fid);
num(i, :) = sscanf(s(i), '%d');
end
And you can even write it more concise
for i = 1:4
num(i, :) = sscanf(fgets(fid), '%d');
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by