【问题标题】:How to read a structured text file and create a structure for it in Matlab?如何在 Matlab 中读取结构化文本文件并为其创建结构?
【发布时间】:2017-03-22 17:14:36
【问题描述】:

我有一个文本文件,其中包含许多时间步长的结果。对于每个时间步,第一行中保存了一些基本信息,然后是一个包含此步骤的其他数据的矩阵。矩阵大小在每个时间步都可以不同,并且不是预定义的。

如何根据这样的文本文件创建结构?

提前致谢!

附:文本文件中的结果如下所示:

 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

【问题讨论】:

    标签: matlab file text structure read-data


    【解决方案1】:

    假设每个新的数据集以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
    

    【讨论】:

    • 感谢Rotem!我正在考虑以一种方式保存结构,即时间步的所有第一行都将位于包含 1x7 元素的子结构“main(i).sub1”中,并且每个时间步下的矩阵将是 main(i ).sub2,它包含 MxN 个元素(M,N 在每个时间步长中变化)。这里 i 指的是第 i 个时间步。而且我猜我的问题中数据的编辑版本不再显示文本文件的清晰结构......
    • 很抱歉错误地编辑了您的帖子(看起来太乱了)。能否请您重新编辑您的帖子。明确如何从文本文件格式中获取 MxN 元素。明确你喜欢的结构。请不要张贴您的文本文件的所有内容 - 发布的内容要足够清楚。
    • 嗨Rotem,没问题!现在结构在帖子中已经很清楚了。
    • 我使用 Octave 测试了我的更新代码,我希望它也适用于 Matlab。
    • 嗨罗特姆!非常感谢你!这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2021-10-17
    相关资源
    最近更新 更多