Rewriting a file?

I need to add an string array (L in the code) to a couple of matlab files ; is there any function working like "writematrix" for .m scripts that we can use to rewrite the code?
Here is my code; the "writematrix" function does not work for it as I am modifying .m scripts:
nL=29;
L="Initial_Mat =[1 2 3];"; %this line should be added to the 29th line of 100 matlab files with fn directory
d=dir(fullfile('C:\Users\...','Testaindex*.m'));
for i=1:numel(d)
fn=fullfile(d(i).folder,d(i).name);
F=readlines(fn);
Ft=[F(1:nL);L;F(nL+1:end)];
writematrix(Ft,fn)
end
Thanks in advance.

1 件のコメント

Stephen23
Stephen23 2023 年 3 月 7 日
"this line should be added to the 29th line of 100 matlab files with fn directory"
Why have so many (nearly) identical Mfiles? Having many many many copies of the same code is a strong indication that you are not using functions effectively: https://en.wikipedia.org/wiki/Code_smell

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

回答 (1 件)

Stephen23
Stephen23 2023 年 3 月 7 日
編集済み: Stephen23 2023 年 3 月 7 日

0 投票

Tip for the future: you are already using READLINES, so scroll right to the bottom of the READLINES help and look at the list of "SEE ALSO" functions. The very first function listed is WRITELINES.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2022b

質問済み:

Zi
2023 年 3 月 7 日

編集済み:

2023 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by