在matlab中提取几个括号的内容?

extract contents of several parenthesis in matlab?

给定如下所示的文本文件,如何使用 MATLAB 提取内容?括号数等于#Pins(第 3 列)。我使用了 textscanregexp 但没有成功。

Net  Name       #Pins   Driver      Recvs

0    o_6_       2   (   1,  13) (   0,   0)

1    i_9_       5   (   6,   1) (   2,   0) (  21,   1) (  28,   2) (  38,   3) 

2    n_n22      4   (  25,  13) (   2,   1) (   3,   7) (  22,   0)

3    [286]      2   (   8,  13) (   1,   2)

您可以使用 fgetl 在循环中逐行读取输入文件。

然后你可以使用strtok来解析当前行,在循环中使用(作为delimiter

下一步是替换每个标记中的 ,),并将结果字符串转换为具有 str2num 的数组。

问题中你没有说明提取出来的数据如何存储,所以有两种可能:

  • 将数据存储到一个struct中,每个字段根据"Net"的ID命名(字段名可以是created dynamically
  • 将数据存储到 cellarray

此方法已在以下代码中实现;代码中的注释应该解释不同的步骤。

% Open the input file
fp_in=fopen('read_bra.txt','rt');
% Initiaize the output cellarray
data_cell{1}=[];
% Initialize the counter for the cellarray
cnt=0;
% Loop to read the input file, line by line
while 1
   % Read a line
   tline = fgetl(fp_in);
   % If the last line of the file has been read
   if(~ischar(tline))
      % Close the input file and exit from the (infinite) loop
      fclose(fp_in);
      break
   end
   % split current line into tokens
   [token, remain]=strtok(tline,'(');
   % The "once" flag:
   %    once == 1: read the ID of the "Net"
   %               reset the temporary matrix "v"
   %               Initialize the output sctructure field
   %               Increment the counter for the cellarray
   %    once == 2: current line is valid (is not empty, and contains data
   %               store the data in the output struct
   %               store the data in the output cellarray
   once=1;
   while(~isempty(remain))
      if(once == 1)
         net_n=sscanf(tline,'%d');
         once=2;
         v=[];
         data_str.(['net_' num2str(net_n)])=[];
         cnt=cnt+1;
      end
      % Split the line wrt "("
      [token, remain]=strtok(remain,'(');
      % Store the data from the current token in the temporary matrix
      v=[v;str2num(strrep(strrep(token,',',''),')',''))];
   end
   % Once all the data of the current line have been extracted
   if(once == 2)
      % Store the data in the output struct
      data_str.(['net_' num2str(net_n)])=v;
      % Store the data in the output cellarray
      data_cell{cnt}=v;
      % Resert the "once" flag
      once=0;
   end
end

根据您发布的输入文件,输出为:

data_str = 

    net_0: [2x2 double]
    net_1: [5x2 double]
    net_2: [4x2 double]
    net_3: [2x2 double]


data_str.net_0
ans =
     1    13
     0     0
data_str.net_1
ans =
     6     1
     2     0
    21     1
    28     2
    38     3
data_str.net_2
ans =
    25    13
     2     1
     3     7
    22     0
data_str.net_3
ans =
     8    13
     1     2


data_cell = 

    [2x2 double]    [5x2 double]    [4x2 double]    [2x2 double]

data_cell{1}
ans =
     1    13
     0     0
data_cell{2}
ans =
     6     1
     2     0
    21     1
    28     2
    38     3
data_cell{3}
ans =
    25    13
     2     1
     3     7
    22     0
data_cell{4}
ans =
     8    13
     1     2