MATLAB Answers

How to replace a string at specific line number in a text file?

5 ビュー (過去 30 日間)
raju patel
raju patel 2020 年 1 月 13 日
コメント済み: Rik 2020 年 1 月 31 日
%%.text file contain%%%
In this
part a
simple
method for
floating‐point
to fixed‐point
%%%new .text file contain%%
In this
part a
simple
method for
/////newly added string///
to fixed‐point
how to replace the string at specific line number?

  3 件のコメント

Guillaume
Guillaume 2020 年 1 月 13 日
line 4 and 5 of what? A text file? A char vector (with some line returns), a scalar string (with some line returns)?
raju patel
raju patel 2020 年 1 月 13 日
I have updated the question.sorry for that.
Rik
Rik 2020 年 1 月 31 日
Adding the names of frequent contributors as tags only proves you have seen several questions, which means you should know how to write a complete question. It generally does not attract actual attention.

サインイン to comment.

回答 (1 件)

Andrew Janke
Andrew Janke 2020 年 1 月 31 日
Try this:
  • Read in the whole text file
  • Split it on \r?\n to break it in to lines
  • Now you have an array of lines, so you can edit line(i) to modify just the i-th line
  • Use strrep or regexprep to modify the contents of that line
  • Use strjoin() to join all the lines back together into a single string
  • Write that string out to your new file

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ

タグが未入力です。


Translated by