【问题标题】:Reading data from a Text File into Matlab array将文本文件中的数据读入 Matlab 数组
【发布时间】:2014-06-09 16:53:00
【问题描述】:

我在使用 Matlab 从 .txt 文件中读取数据时遇到困难。

我必须使用 .txt 文件中的数据在 Matlab 中创建一个 200x128 维数组。这是一项重复性任务,需要自动化。

.txt文件的每一行都是a+ib形式的复数,形式为a[space]b。我的文本文件示例:

文本文件链接:Click Here

(0)

1.2 2.32222

2.12 3.113

.

.

.

3.2 2.22

(1)

4.4 3.4444

2.33 2.11

2.3 33.3

.

.

.

(2)

.

.

(3)

.

.

(199)

.

.

我有行数 (X),在 .txt 文件中用括号括起来。我的最终矩阵的大小应为 200x128。在每个 (X) 之后,正好有 128 个复数。

【问题讨论】:

    标签: matlab parsing matlab-figure text-parsing string-parsing


    【解决方案1】:

    这就是我要做的。首先,从文本文件中删除“(0)”类型的行(甚至可以使用简单的 shell 脚本)。我把它放入名为 post2.txt 的文件中。

    # First, load the text file into Matlab:
    A = load('post2.txt');
    
    # Create the imaginary numbers based on the two columns of data:
    vals = A(:,1) + i*A(:,2);
    
    # Then reshape the column of complex numbers into a matrix
    mat = reshape(vals, [200,128]);
    

    mat 将是一个 200x128 复杂数据的矩阵。显然,此时您可以围绕此循环多次执行此操作。

    希望对您有所帮助。

    【讨论】:

    • 我不太擅长 shell 脚本,最后我写了一个 25 行的 python 代码来创建 post2。如果您可以分享如何在 shell 上执行此操作,那将在将来有所帮助。再次感谢!此外,您的代码应该是 reshape(vals, [128,200])' 。
    • @Suddev:如果您在基于 unix 的系统上,您可以使用反向 grep,例如: grep -v '^(' post.txt > post2.txt 这会输出所有不符合要求的行'不以括号开头。
    • 我在 Windows 上,我在基于 unix 的机器上尝试了你所说的。它有效 - 再次感谢!
    【解决方案2】:

    您可以使用以下函数读取数据:

    function data = readData(aFilename, m,n)
    
    % if no parameters were passed, use these as defaults:
    if ~exist('aFilename', 'var')
        m = 128;
        n = 200;
        aFilename = 'post.txt';
    end
    
    % init some stuff:
    data= nan(n, m);
    formatStr = [repmat('%f', 1, 2*m)];
    
    % Read in the Data:
    fid = fopen(aFilename);
    for ind = 1:n
        lineID = fgetl(fid);
        dataLine = fscanf(fid, formatStr);
        dataLineComplex = dataLine(1:2:end) + dataLine(2:2:end)*1i;
        data(ind, :) = dataLineComplex;
    end
    fclose(fid);
    

    (编辑)可以通过在格式字符串中包含(1) 部分并将它们丢弃来改进此功能:

    function data = readData(aFilename, m,n)
    
    % if no parameters were passed, use these as defaults:
    if ~exist('aFilename', 'var')
        m = 128;
        n = 200;
        aFilename = 'post.txt';
    end
    
    % init format stuff:
    formatStr = ['(%*d)\n' repmat('%f%f\n', 1, m)];
    
    % Read in the Data:
    fid = fopen(aFilename);
    
    data = fscanf(fid, formatStr);
    data = data(1:2:end) + data(2:2:end)*1i;
    data = reshape(data, n,m);
    
    fclose(fid);
    

    【讨论】:

    • 我将结合你的答案和@brechmos 的答案来解决这个问题。我希望我能尽快做到。
    • @Steve,为什么要使用“nan(n, m)”。做“zeros(n, m)”可能会更好。这是更传统的,但也许我错过了一些东西。 (取决于我想的假设)。我怀疑这样的循环会比使用内部加载函数效率低。
    • @Suddev:如果您在基于 unix 的系统上,您可以使用反向 grep,例如: grep -v '^(' post.txt > post2.txt 这将输出 不要以括号开头。
    • @brechmos 关于load 命令尚不清楚哪个更快,尽管您很可能是正确的。 MATLAB 的 load 是为灵活性而构建的,而对于较大的值,循环速度很慢。我想使用格式字符串formatStr = ['(%*d)' repmat('%f', 1, 2*m)];,然后一次加载整个文件----可能相当快,但它在第一行记录之后挂起,所以我把它扔在一个循环中。由于文件很小,无论如何只有一点点惩罚。要让这种特定格式发挥作用,这似乎是一个足够快的解决方案。
    • @brechmos 至于nan 值,我经常喜欢这样做,以便如果代码有索引错误,那些nan 值会为将来的处理提供一个邪恶的、令人不快的指标。另外,我通过添加\n 来修复格式字符串,并在答案中添加了一个无循环示例。为你
    猜你喜欢
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2012-02-27
    • 2017-07-16
    相关资源
    最近更新 更多