How to replace different variables with a single variable in a text file?

1 回表示 (過去 30 日間)
Tiasa Ghosh
Tiasa Ghosh 2018 年 5 月 14 日
コメント済み: Tiasa Ghosh 2018 年 5 月 15 日
Hello everybody! I am not so used to editing and working with .txt files in MATLAB. I am looking for ways to replace multiple set of characters like '=' , '+', '-' with any single character like '%'. I guess I need to use regexprep for this purpose, but I am not able to figure it out for the .txt file. Any help will be appreciated. Thanks
  2 件のコメント
Bob Thompson
Bob Thompson 2018 年 5 月 14 日
What do you mean by not being able to figure it out for the .txt file?
Tiasa Ghosh
Tiasa Ghosh 2018 年 5 月 15 日
I meant if the input is a cell array, it can be worked out by specifying each cell as cell{} and replacing, but how should I do it for a .txt file with several lines?

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

採用された回答

Akira Agata
Akira Agata 2018 年 5 月 15 日
Here is a simple example on how to use regexprep to replace '=','+','-' with '%'.
strIn = 'Please replace multiple set of characters like =, +, - with %';
strOut = regexprep(strIn,'[=+-]','%');
The result becomes:
>> strOut
strOut =
'Please replace multiple set of characters like %, %, % with %'
  2 件のコメント
Tiasa Ghosh
Tiasa Ghosh 2018 年 5 月 15 日
Thank you for your response. I am trying to work with a .txt file here. so if the input is a .txt file, how does the format of the regexprep work? I tried this:
fid = fopen('replacedFile.txt','wt');
fprintf(fid,'%s',regexprep('myFile.txt','[=+()]','%'));
fclose(fid);
I know it is wrong since it just creates a .txt file with 'myFile.txt' as one line.
Tiasa Ghosh
Tiasa Ghosh 2018 年 5 月 15 日
Silly mistake on my part I used fileread first to get the inputdata. Thank you again.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by