Reputation: 43
Here is what my data file looks like:
1
num num num
num num num
num num num
num num num
num num num
2
num num num
num num num
num num num
num num num
num num num
3
num num num
num num num
num num num
num num num
num num num
. . .
1000
num num num
num num num
num num num
num num num
num num num
'num' refers to a different float number, and 1,2,3,...,1000 are also part of the file, occupying one line each. What I want to do is, I need a loop from time step 1 to 1000, and during each step, I need to read the 3-column float number block below it as three column vectors. Then I proceed to the next time step, and read the block below, until I finish reading all.
How could I do this file reading with Matlab? In short, what I want to do is to read line 2 to line 6 as a matrix, then line 8 to line 12 as a matrix, then line 14 to 18 as a matrix, and so on...
Thanks!
Upvotes: 0
Views: 1090
Reputation: 32084
You can read the text file as follows:
%Open text file
f = fopen('num.txt', 'r');
num_matrices = 1000;
%Initialize cell array - hold matrices.
C = cell(num_matrices, 1);
for i = 1:num_matrices
%Read index (to be ignored).
idx = fscanf(f, '%f', 1);
%Read 6x3 matrix into A
A = fscanf(f, '%f', [3, 6])';
%Store matrix in cell array C.
C{i} = A;
end
fclose(f);
Refer to https://www.mathworks.com/help/matlab/ref/fscanf.html for fscanf
documentation.
Upvotes: 1