在matlab中提取几个括号的内容?(extract contents of several parenthesis in matlab?)

编程入门 行业动态 更新时间:2024-10-26 06:26:49
在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)

Given a text file as shown below, how can I extract the contents using MATLAB? The number of parenthesis is equal to #Pins (column 3). I used textscan and regexp but was unsuccessful.

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的数组。

在问题中,您没有指定如何存储提取的数据,因此有两种可能性:

将数据存储到结构中,每个字段根据“Net”的ID命名(可以动态创建字段的名称) 将数据存储到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

You can use fgetl to read line by line the input file within a loop.

Then you can use strtok to parse the current line, using ( as delimiter within a loop.

Next step is to replace , and ) in each token and convert the resulting string into an array with str2num.

In the question you did not specify how to store the data extracted, so two possibilities could be:

store the data into a struct, each field is named according to the ID of the "Net" (the names of the field can be created dynamically store the data into a cellarray

This approach has been iimplemented in the following code; the comments in the code should explain the different steps.

% 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

Given the input file you've posted, the output is:

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

更多推荐

本文发布于:2023-08-07 22:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   括号   内容   matlab   contents

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!