フィルターのクリア

How to replace two columns in a txt file?

2 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2020 年 9 月 25 日
コメント済み: Sudhakar Shinde 2020 年 9 月 30 日
Dear All,
I have a txt file which contains strings and numerical columns. Here are some samples of this txt file:
3 'CHSTRSVC' 345.00 2 0.00 0.00 1 1 1.0269 -11.382 1
4 'ORRINGTN' 345.00 1 0.00 0.00 1 1 1.0191 -16.365 1
3146 'ORRINGTN' 345.00 1 0.00 0.00 1 1 1.0162 -13.797 1
3147 'ORRINGTN' 345.00 1 0.00 0.00 1 1 1.0179 -13.586 1
......
I want to replace the values in the last 2 columns with updated values, and save the updated values in the same txt file. So the txt file has only two columns updated. For example, in the first row, I want to replace 1.0269 with 0.9987 and -11.382 with -10.5634.
Is there a simple way to do it?
Thanks.
Benson

採用された回答

Sudhakar Shinde
Sudhakar Shinde 2020 年 9 月 28 日
This could help you:
fid = fopen('test.txt');
C = textscan(fopen('test.txt'),'%s','delimiter','\n');
C=strrep(C{1}, '1.0269','0.9987');
C=strrep(C, '-11.382','-10.5634');
fid = fopen('test.txt','w');
  4 件のコメント
Benson Gou
Benson Gou 2020 年 9 月 29 日
Hi, Sudhakar,
Thanks a lot for your great help. I solved my problem.
Best regards,
Benson
Sudhakar Shinde
Sudhakar Shinde 2020 年 9 月 30 日
Welcome

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOceanography and Hydrology についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by