【问题标题】:How to find a string which is not completely known in a text file and replace it in matlab?如何在文本文件中找到一个不完全已知的字符串并在matlab中替换它?
【发布时间】:2015-12-16 09:11:57
【问题描述】:

我有一个相当大的文本文件,其中像下面这样的字符串重复了 50 次。

NODE LABEL="NODE-1", LENGTH=(0.001,0.69, 1.805, 5, 10, \

这些重复字符串之间的唯一区别是=( 之后的第一个数字。

我的意思是字符串如下:

NODE LABEL="NODE-1", LENGTH=(0.001,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-2", LENGTH=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-3", LENGTH=(0.067,0.69, 1.805, 5, 10, \

我在 MATLAB 中有一个类似于以下的向量:

d =[0.010
    0.120
    0.140]

首先,我想识别那些包含上述字符串的行。然后,我需要将文本文件每一行中的第一个数字替换为向量d 中对应的数字。 这意味着,我的输出文本文件看起来像:

Bla Bla Bla Bla
NODE LABEL="NODE-1", LENGTH=(0.010,0.69, 1.805, 5, 10, \
Bla Bla Bla Bla
Bla Bla Bla Bla
NODE LABEL="NODE-2", LENGTH=(0.012,0.69, 1.805, 5, 10, \
Bla Bla Bla Bla
NODE LABEL="NODE-3", LENGTH=(0.014,0.69, 1.805, 5, 10, \

我该怎么做?

我的问题的本质是,如何在文本文件中找到像LENGTH=(NUMBER, 这样的字符串,而NUMBER 是未知的。

【问题讨论】:

  • 查找正则表达式。您完全可以将它们用于此目的。

标签: matlab text replace find


【解决方案1】:

在您的问题中,不清楚输入文件是否可以包含没有“LENGTH=(”字符串的行。

我认为可以。

根据您指定的输入文件行的格式,您可以:

  • 使用findstr 检查是否存在“LENGTH=(”字符串
  • 在行中找到要替换的号码的位置,再次使用findstr
  • 将字符串的一部分替换为“d”数组中的数字,将其转换为字符串

这个方法已经在下面的代码中实现了;还添加了对“d”数组中值的数量与要修改的行数的检查。

我使用了一个输入文件,其中包含没有“LENGTH=(”字符串的行;在这种情况下,原始字符串被写入 OUTPUT 文件中。

输入文件

NODE LABEL="NODE-1", LENGTH=(0.001,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-2", LENGTH=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-3", WIDTH=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-4", XXXX=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-5", YYYYYYYY=(0.067,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-6", LENGTH=(0.005,0.69, 1.805, 5, 10, \

脚本

% Define the array with replacing values
d =[0.010
   0.120
   0.140];
% Get the number of possible replaceable input file strings
n_val=length(d);
% Open INPUT and OUTPUT files
fp_in=fopen('in_file.txt','rt')
fp_out=fopen('out_file.txt','wt')
% Initialize counter
cnt=1;
% Read the input file line by line
while(1)
   tline = fgetl(fp_in)
   if(ischar(tline))
      % If current input line contains the "LENGTH=(" string, try to
      % replace the number
      if(findstr(tline,'LENGTH=('))
         % If "d" array does not contains enough values, generate error
         % message
         if(cnt > n_val)
            error('Not enough values in "d" array')
         end
         % Replace the number in the string with a value from "d" array
         b=findstr(tline,'(')
         c=findstr(tline,',')
         tline(b+1:c(2)-1)=sprintf('%4.3f',d(cnt))
         % Print the modified line in the OUTPUT file
         fprintf(fp_out,'%s\n',tline)
         % Increment the counter
         cnt=cnt+1
      else
         % If the current line does not contains the "LENGTH=(" string,
         % writhe the line in the OUTPUT file as it is 
         fprintf(fp_out,'%s\n',tline)
      end
   else
      % Stop reading INPUT file
      break
   end
end
% Close INPUT and OUTPUT files
fclose(fp_in);
fclose(fp_out);

输出文件

NODE LABEL="NODE-1", LENGTH=(0.010,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-2", LENGTH=(0.120,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-3", WIDTH=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-4", XXXX=(0.005,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-5", YYYYYYYY=(0.067,0.69, 1.805, 5, 10, \
NODE LABEL="NODE-6", LENGTH=(0.140,0.69, 1.805, 5, 10, \

希望这会有所帮助。

【讨论】:

  • 好的,它工作正常。使用权限“w”和“wt”打开输出文件有什么区别?当我使用第一个时,在同一行的输出文件中连续打印行。同时,使用最后一个权限在下一个(新)行上打印 tline。
  • Addind t 指定文件必须在text mode 中打开。这不应该对您的输出写在一行上这一事实负责。在write 添加t 意味着在输出中的任何换行符之前插入一个回车符(参考fopen documentation)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
相关资源
最近更新 更多