matlab函数替换已知字符之间的字符串的最后一部分

用户名

我有一个文本文件TF,其中包含一组以下类型的字符串:

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.T", 
"linStru.twoZoneBuildingStructure.north.vol.Xi[1]", 
"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.T",
"linStru.twoZoneBuildingStructure.south.vol.Xi[1]", " 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.T[1]",
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.T[2]",

给定行L,从末尾开始,让子字符串s表示字符串,“和first之间的部分。

更清楚地说,对于L = 1:s = T,对于L = 2:s = Xi [1],对于L = 5:s = T [1],依此类推。

给定上述格式的文本文件TF,我想编写一个MATLAB函数,该函数采用TF并将每行上的对应s替换为der。

例如,该函数应按以下方式更改上述字符串:

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.der(T)", 
"linStru.twoZoneBuildingStructure.north.vol.der(Xi[1])", 
"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.der(T)",
"linStru.twoZoneBuildingStructure.south.vol.der(Xi[1])", " 
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.der(T[1])",
"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.der(T[2])",

如何编写这样的函数?

雷瑞恩

我看到"您的文本文件第四行上有一个小的错字。我将删除它以使事情变得更简单。

这样,我可以看到的最简单的方法是遍历所有字符串,删除单引号,然后在字符串中找到最后一个.出现的点。提取此子字符串,然后der()在该字符串之间手动插入in。假设这些字符串在名为的文本文件中functions.txt,则可以使用textread读取单个字符串来读取文本文件因此:

names = textread('functions.txt', '%s');

names现在应该是名称的单元格数组,其中每个元素都是用双引号封装的每个字符串。使用findstr以提取其中.的位置,然后提取的地方,这是最后的位置。提取此子字符串,然后将其替换为der()换一种说法:

out_strings = cell(1, numel(names)); %// To store output strings
for idx = 1 : numel(names)
     %// Extract actual string without quotes and comma
     name_str = names{idx}(2:end-2);

     %// Find the last dot
     dot_locs = findstr(name_str, '.');

     %// Last dot location
     last_dot_loc = dot_locs(end);

     %// Extract substring after dot
     last_string = name_str(last_dot_loc+1:end);

     %// Create new string 
     out_strings{idx} = ['"' name_str(1:last_dot_loc) 'der(' last_string ')",'];
end

这是我得到的输出:

celldisp(out_strings)

out_strings{1} =

"linStru.twoZoneBuildingStructure.north.airLeakage.senTem.der(T)",


out_strings{2} =

"linStru.twoZoneBuildingStructure.north.vol.der(Xi[1])",


out_strings{3} =

"linStru.twoZoneBuildingStructure.south.airLeakage.senTem.der(T)",


out_strings{4} =

"linStru.twoZoneBuildingStructure.south.vol.der(Xi[1])",


out_strings{5} =

"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer1Nf.der(T[1])",


out_strings{6} =

"linStru.twoZoneBuildingStructure.north_ext.layMul.nMat[1].monoLayer2Nf.der(T[2])",

您要做的最后一件事是将每一行文本写入文本文件。您可以fopen用来打开要写入的文件。fopen返回与您要写入的文件关联的文件ID。然后fprintf,您可以使用此文件ID打印字符串并为每个字符串命名换行符。然后,使用fclose具有相同文件ID的文件来关闭文件。这样,如果我们要输出一个名为的文本文件functions_new.txt,则可以执行以下操作:

%// Open up the file and get ID
fid = fopen('functions_new.txt', 'w');

%// For each string we have...
for idx = 1 : numel(out_strings)
    %// Write the string to file and make a new line
    fprintf(fid, '%s\n', out_strings{idx});
end

%// Close the file
fclose(fid);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章