【问题标题】:Extract variables from a textfile in matlab从matlab中的文本文件中提取变量
【发布时间】:2014-04-10 02:10:21
【问题描述】:

我有一个包含仪器输出数据的大文本文件(约 3500 行)。这包括来自不同测量的重复条目,它们的格式如下:

* LogFrame 开始 *

变量名1:值

变量名2:值

...

变量名35:值

* LogFrame 结束 *

每个逻辑框架包含 35 个变量。从这些我想提取两个,'VName'和'EMGMARKER'并将关联的值放在matlab数组的列中,(即数组应该是(VName,EMGMARKER),然后我可以将其与来自另一个输出文件的数据相关联我已经在 matlab 数组中了。我不知道从哪里开始以便从这个文件中提取变量,因此到目前为止我在互联网上的搜索一直不成功。对此的任何建议将不胜感激。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    你可以使用 textscan:

    C = textscan(file_id,'%s %f');
    

    然后你像这样提取感兴趣的变量:

    VName_indices = strcmp(C{1},'VName:');
    VName = C{2}(VName_indices);
    

    【讨论】:

      【解决方案2】:

      如果变量名,如 'VName' 和 'EMGMARKER' ,总是相同的,你可以通读文件并搜索包含它们的行并从中提取数据。我不知道它们包含什么值,因此在提取它们时可能必须使用单元格而不是数组。

      fileID = fopen([target_path fname]); % open .txt file
      
      % read the first line of the specified .txt file
      current_line = fgetl(fileID);
      counter = 1;
      
      % go through the whole file
      while(ischar(current_line))
      
      if ~isempty(strfind(current_line, VName))
           % now you find the location of the white space delimiter between name and value
           d_loc = strfind(current_line,char(9));% if it's tab separated - so \t
           d_loc = strfind(current_line,' ');% if it's a simple white space
           variable_name{counter} = strtok(current_line);% strtok returns everything up until        the first white space
           variable_value{counter} = str2double(strtok(current_line(d_loc(1):end)));% returns everything form first to second white space and converts it into a double
      end
      
      
      % same block for EMGMARKER
      
      counter = counter+1;
      current_line = fgetl(fileID);% next line
      end
      

      您对 EMGMARKER 执行相同操作,并且您拥有包含所需数据的单元格。

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 2019-06-25
        • 1970-01-01
        • 2019-01-06
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多