I have some 50+ conf files that I am working. I need to find and replace various variable for all these files. For example, I'd like to find the line
Amplitude = 100; and replace it to: Amplitude = 200; for all files.
I've searched in online and found the solution only for one file. I'm looking for a way to do that in matlab. Any ideas?

 採用された回答

Jonathon Gibson
Jonathon Gibson 2018 年 6 月 28 日
編集済み: Jonathon Gibson 2018 年 6 月 28 日

1 投票

If you're on a Unix system, and currently in the directory with the conf files, you can run from MATLAB:
!sed -i 's/Amplitude = 100;/Amplitude = 200;/g' ./*
Which will check every file in the current directory (./*) for the first regular expression (Amplitude = 100;) and replace it with the second regular expression (Amplitude = 200;). The ! just makes it the same as entering the command directly into the system's terminal.

5 件のコメント

Namira
Namira 2018 年 6 月 28 日
Thanks. I am using Linux. It does not work in Linux system.
Walter Roberson
Walter Roberson 2018 年 6 月 28 日
!sed -i 's/Amplitude = 100;/Amplitude = 200;/g' ./*
Namira
Namira 2018 年 6 月 28 日
Thanks. I run the command in the command window in Matlab. It does not change the text. Did I do anything wrong?
Walter Roberson
Walter Roberson 2018 年 6 月 28 日
sed is sensitive to spacing and upper/lower case. For example if there were two spaces between the = and the 100, then the above command would not match. The version that is not sensitive to spacing is
!sed -i 's/Amplitude\s*=\s*100;/Amplitude = 200;/g' ./*
Namira
Namira 2018 年 6 月 28 日
Thank you so much. It's working now.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2018 年 6 月 28 日

編集済み:

2018 年 6 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by