【问题标题】:retriving a text file content information in Matlab在 Matlab 中检索文本文件内容信息
【发布时间】:2018-11-26 17:12:38
【问题描述】:

我有一个包含 300 行的 .txt 文件。例如第一行是:

ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16

或者第二行是:

ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18

只有“时间”和“文件”值是数字,其他是字符串。

我想将整 300 行的“正确”、“时间”、“猜测”、“动作”和“文件”的值存储在不同的变量中(比如一些数组)。

如何在 Matlab 中做到这一点?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    选项 1:

    您可以将textscan 与以下formatSpec 一起使用:

    formatSpec = 'ANSWER: correct:%s time:%f guess:%s action:%s file: %f';
    data = textscan(fileID,formatSpec,'Delimiter',',');
    

    其中fileIDfopen获取的文件标识符。

    选项 2:

    另一种选择是使用readtable,具有上述格式(直接使用文件名,没有文件ID):

    data = readtable('53485991.txt','Format',formatSpec,'Delimiter',',',...
        'ReadVariableNames',false);
    % the next lines are just to give the table variables some meaningful names:
    varNames = strsplit(fmt,{'ANSWER',':','%s',' ','%f'});
    data.Properties.VariableNames = varNames(2:end-1);
    

    结果(忽略值,因为我在玩这个例子时有点搞砸了):

    data =
      4×5 table
        correct     time          guess         action    file
        _______    ______    _______________    ______    ____
        'yes'      6.8888    'Lay'              'Lay'      16 
        'no'       7.8762    'Put on top'       'Stir'     18 
        'no'       7.1503    'Put on bottom'    'Stir'      3 
        'no'        7.151    'go'               'Stir'    270 
    

    选项 2 的优点是表是 a much more convenient way 来保存这些数据,而不是元胞数组(这是 textscan 的输出)。

    【讨论】:

    • 学到了一些新东西。 textscan 很优雅。
    • 感谢您的回答。当我使用“选项1”时,数据将以这样的形式出现:data = {1x1 cell} [6.8807] {1x1 cell} {1x1 cell} [0x1 double],但是我怎样才能检索我的时间等信息,正确等来自这些数据?
    • 第一,最好使用选项2。第二,你应该输入:correct = data{1}time = data{2}等等直到5(文件),用于数据中的每个字段。
    • 非常感谢您的帮助和建议 :)
    • 你必须检查你的数据格式是否正确,正如你上面写的那样。另一种选择是编码问题,但很难从这个错误中的信息中分辨出来。
    【解决方案2】:

    使用fgetl 获取文件的一行,使用while 循环读取所有行。

    对于每一行,使用regexp 通过:, 分隔符将字符串划分为单元格。然后,使用strip 删除每个单元格的前导和尾随空格。

    解决办法如下:

    f = fopen('a.txt');
    
    aline = fgetl(f);
    i = 1;
    while ischar(aline)
        content = strip(regexp(aline,':|,','split'));
        correct{i} = content{3};
        time(i) = str2double(content{5});
        guess{i}= content{7};
        action{i} = content{9};
        file(i) = str2double(content{11});
        i = i + 1;
        aline = fgetl(f);
    end
    
    fclose(f);
    

    例子:

    假设一个.txt 文件看起来像这样

    ANSWER: correct: yes, time: 6.880674, guess: Lay, action: Lay, file: 16
    ANSWER: correct: no, time: 7.150422, guess: Put on top, action: Stir, file: 18
    

    执行脚本后,结果是

    correct =
      1×2 cell array
        'yes'    'no'
    
    time =
        6.8807    7.1504
    
    guess =
      1×2 cell array
        'Lay'    'Put on top'
    
    action =
      1×2 cell array
        'Lay'    'Stir'
    
    file =
        16    18
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 2017-09-16
      • 1970-01-01
      • 2018-02-27
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      相关资源
      最近更新 更多