filereadで読み込んだファイルを別名で保存するコマンドを知りたいです。
5 views (last 30 days)
Show older comments
YOKOI Sayoko
on 24 Apr 2024
Commented: Akira Agata
on 25 Apr 2024
テキストファイル(240313.rtm)を読み込んで、一部を編集し、別名で保存することをしたいです。
saveではうまくいかず、最適な方法があればお教えください。
>> a=fileread('240313.rtm')
a =
'EEM-RTM
5 1
240313_3F_4-5GHz-nn
fffe9
====MATERIAL====
2 xxx 0.25 0.02 "ceil"
2 yyy 0.16 0.025 "wall"
2 zzzz 0.04 0.0265 "floor"
2 4 0 0.15 "concrete"
0
'
>> b=replace(a, 'xxx', '4.0')
b =
'EEM-RTM
5 1
240313_3F_4-5GHz-nn
fffe9
====MATERIAL====
2 4.0 0.25 0.02 "ceil"
2 yyy 0.16 0.025 "wall"
2 zzzz 0.04 0.0265 "floor"
2 4 0 0.15 "concrete"
0
'
save('240313-1.rtm', 'b') %これでは保存できない
0 Comments
Accepted Answer
Atsushi Ueno
on 24 Apr 2024
fprintf 関数でASCIIファイルに文字列を書き込めます。
movefile 240313.txt 240313.rtm % 都合で拡張子rtmのファイルをupload出来ない⇒拡張子をrtmに変更
a = fileread('240313.rtm');
b = replace(a, 'xxx', '4.0');
fileID = fopen('240313-1.rtm','w');
fprintf(fileID,'%s\n',b); % これなら保存できる
fclose(fileID);
type 240313-1.rtm
2 Comments
Akira Agata
on 25 Apr 2024
+1
% ファイル読み込み
s1 = readlines("240313.rtm");
% xxx を 4.0 に置換
s2 = replace(s1, "xxx", "4.0");
% 別ファイル名で保存
writelines(s2, "240313-1.rtm");
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!