フィルターのクリア

Read a text file that includes numbers and strings and edit it in different places

1 回表示 (過去 30 日間)
MD MAHABUBUR ROHOMAN
MD MAHABUBUR ROHOMAN 2022 年 11 月 8 日
コメント済み: Walter Roberson 2022 年 11 月 9 日
I have a text file that is something like the attached picture. I want to read this file and whenever I come to the line 67 my code will add lines like
67 and 68 and follows as,
67 *Nset, nset=node1, instance=Part-1-1
68 1
69 *Nset, nset=node2, instance=Part-1-1
70 2
71 *Nset, nset=node3, instance=Part-1-1
72 3
and onwards...
Basically it will add two lines after it counters the line 67th.
67

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 11 月 8 日
readlines() to get a string array. Index 1:66, then several entries with the new lines, then index 68:end. writelines() the results.
  2 件のコメント
MD MAHABUBUR ROHOMAN
MD MAHABUBUR ROHOMAN 2022 年 11 月 8 日
Thanks for your response. What if I just want to write a text file like the following,
*Nset, nset=node1, instance=Part-1-1
1
*Nset, nset=node2, instance=Part-1-1
2
....
...
..... and onwards?
Walter Roberson
Walter Roberson 2022 年 11 月 9 日
nodenums = 1:3;
part1 = "*Nset, nset=node";
part2 = ", instance=Part-1-1";
output1 = part1 + nodenums.' + part2;
output2 = string(nodenums.');
output = reshape([output1, output2].', [], 1)
output = 6×1 string array
"*Nset, nset=node1, instance=Part-1-1" "1" "*Nset, nset=node2, instance=Part-1-1" "2" "*Nset, nset=node3, instance=Part-1-1" "3"
filecontents = readlines(FILENAME);
newcontents = [filecontents(1:66); output; filecontents(68:end)];
writelines(newcontents, NEWFILENAME);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by