如何读取结构化文本文件并在 Matlab 中为其创建结构?
How to read a structured text file and create a structure for it in Matlab?
我有一个文本文件,其中包含许多时间步长的结果。对于每个时间步,一些基本信息保存在第一行,然后是一个包含该步骤其他数据的矩阵。矩阵大小在每个时间步可能不同,并且未预定义。
如何基于这样的文本文件创建结构?
提前致谢!
P.S。文本文件中的结果如下所示:
time= 4.3750000000000001E-004 3 7 4 1 4.9999989999999998E-004
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 5.6569E+08 7.5717E+08 5.6569E+08 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
time= 5.0000000000000001E-004 3 5 3 0 4.9999989999999998E-004
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 2.3593E+08 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
time= 1.8125000000000001E-003 3 3 3 1 1.8749999000000001E-003
0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 3.9138E+07 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00
假设每个新的数据集以 time=
开始并以间隔行结束,您可以使用以下代码示例:
%Open text file for reading (assume file name is 'Data.txt').
f = fopen('Data.txt', 'r');
%Initialize main (store data) to empty matrix.
main = [];
%Initialize index to 1
i = 1;
while (~feof(f))
%Read single line from text file (as long string).
S = fgets(f);
if (strfind(S, 'time') > 0)
%Remove 'time= ' from the beginning of S.
S = strrep(S, 'time=', '');
%Convert string to array of numbers.
T = sscanf(S, '%f ');
%Store vector T to main(i).sub1
main(i).sub1 = T';
%Set A to empty matrix - prepare for filling with new data.
A = [];
%Read next line from text file (as long string).
S = fgets(f);
end
%Convert string to array of numbers.
L = sscanf(S, '%f ');
if (isempty(L) || feof(f))
%Store matrix A to main(i).sub2
main(i).sub2 = A;
%Advance i (data index) by 1.
i = i + 1;
else
%In case A is not empty, concatenate T to bottom of A.
A = [A; L'];
end
end
%Close file.
fclose(f);
结果:
>>main(1)
sub1 =
4.3750e-04 3.0000e+00 7.0000e+00 4.0000e+00 1.0000e+00 5.0000e-04
sub2 =
0 0 0 0 0 0 0
0 0 565690000 757170000 565690000 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
>>main(2)
sub1 =
0.00050 3.00000 5.00000 3.00000 0.00000 0.00050
sub2 =
0 0 0 0 0
0 0 235930000 0 0
0 0 0 0 0
>>main(3)
sub1 =
0.0018125 3.0000000 3.0000000 3.0000000 1.0000000 0.0018750
sub2 =
0 0 0
0 39138000 0
0 0 0
我有一个文本文件,其中包含许多时间步长的结果。对于每个时间步,一些基本信息保存在第一行,然后是一个包含该步骤其他数据的矩阵。矩阵大小在每个时间步可能不同,并且未预定义。
如何基于这样的文本文件创建结构?
提前致谢!
P.S。文本文件中的结果如下所示:
time= 4.3750000000000001E-004 3 7 4 1 4.9999989999999998E-004
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 5.6569E+08 7.5717E+08 5.6569E+08 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
time= 5.0000000000000001E-004 3 5 3 0 4.9999989999999998E-004
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 2.3593E+08 0.0000E+00 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
time= 1.8125000000000001E-003 3 3 3 1 1.8749999000000001E-003
0.0000E+00 0.0000E+00 0.0000E+00
0.0000E+00 3.9138E+07 0.0000E+00
0.0000E+00 0.0000E+00 0.0000E+00
假设每个新的数据集以 time=
开始并以间隔行结束,您可以使用以下代码示例:
%Open text file for reading (assume file name is 'Data.txt').
f = fopen('Data.txt', 'r');
%Initialize main (store data) to empty matrix.
main = [];
%Initialize index to 1
i = 1;
while (~feof(f))
%Read single line from text file (as long string).
S = fgets(f);
if (strfind(S, 'time') > 0)
%Remove 'time= ' from the beginning of S.
S = strrep(S, 'time=', '');
%Convert string to array of numbers.
T = sscanf(S, '%f ');
%Store vector T to main(i).sub1
main(i).sub1 = T';
%Set A to empty matrix - prepare for filling with new data.
A = [];
%Read next line from text file (as long string).
S = fgets(f);
end
%Convert string to array of numbers.
L = sscanf(S, '%f ');
if (isempty(L) || feof(f))
%Store matrix A to main(i).sub2
main(i).sub2 = A;
%Advance i (data index) by 1.
i = i + 1;
else
%In case A is not empty, concatenate T to bottom of A.
A = [A; L'];
end
end
%Close file.
fclose(f);
结果:
>>main(1)
sub1 =
4.3750e-04 3.0000e+00 7.0000e+00 4.0000e+00 1.0000e+00 5.0000e-04
sub2 =
0 0 0 0 0 0 0
0 0 565690000 757170000 565690000 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
>>main(2)
sub1 =
0.00050 3.00000 5.00000 3.00000 0.00000 0.00050
sub2 =
0 0 0 0 0
0 0 235930000 0 0
0 0 0 0 0
>>main(3)
sub1 =
0.0018125 3.0000000 3.0000000 3.0000000 1.0000000 0.0018750
sub2 =
0 0 0
0 39138000 0
0 0 0