Clear Filters
Clear Filters

filereadで読​み込んだファイルを別​名で保存するコマンド​を知りたいです。

3 views (last 30 days)
YOKOI Sayoko
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')  %これでは保存できない

Accepted Answer

Atsushi Ueno
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
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
  2 Comments
YOKOI Sayoko
YOKOI Sayoko on 25 Apr 2024
ありがとうございました。うまく実行することができました。
Akira Agata
Akira Agata on 25 Apr 2024
+1
テキストファイルを読み書きするための関数である readlines / writelines を使っても実現可能です。
% ファイル読み込み
s1 = readlines("240313.rtm");
% xxx を 4.0 に置換
s2 = replace(s1, "xxx", "4.0");
% 別ファイル名で保存
writelines(s2, "240313-1.rtm");

Sign in to comment.

More Answers (0)

Categories

Find more on 環境と設定 in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!