Edit txt file and create new files

2 ビュー (過去 30 日間)
Ellen Robson
Ellen Robson 2020 年 12 月 10 日
コメント済み: Rik 2020 年 12 月 22 日
I have a large txt file that is NOT in rows and columns (see attached).
In the file, it outlines values for various parameters:
num: 1 gsi: 21.8 mi: 7 disturbance: 0 use_gsi_mi_d: 1
num: 2 gsi: 1 mi: 5 disturbance: 0.1 use_gsi_mi_d: 1
num: 3 gsi: 2 mi: 6 disturbance: 0.2 use_gsi_mi_d: 1
num: 4 gsi: 3 mi: 7 disturbance: 0.3 use_gsi_mi_d: 1
num: 5 gsi: 4 mi: 8 disturbance: 0.4 use_gsi_mi_d: 1
I want to create 1000 versions of this txt file and with these values changed in each. The parameter values will be loaded into MATLAB as vectors.
How can I do this?
  1 件のコメント
Rik
Rik 2020 年 12 月 10 日
It is very difficult to edit files by code, but very easy to overwrite them. So if it is possible to hold a file in memory, you can load it to a Matlab variable, make the edit, and write out.
Did you try that?

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

回答 (1 件)

Madhav Thakker
Madhav Thakker 2020 年 12 月 22 日
+1 to Rik's comment. It is easier to edit files by code.
You can read the entire text file using fileread and read the parameters separately using readtable. You can then modify the values in the table and writeout.
Hope this helps.
  1 件のコメント
Rik
Rik 2020 年 12 月 22 日
Instead of fileread I would suggest readlines, or my readfile function from the FEX (which will also work pre-R2020b).

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

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by