How is to read and write a text file in matlab?

1 回表示 (過去 30 日間)
Azade Jamshidi
Azade Jamshidi 2018 年 7 月 19 日
コメント済み: Azade Jamshidi 2018 年 7 月 19 日
I am trying for reading and writing a text file (a notepad file with 44 lines) in Matlab. I want to read only lines 2 to 40 of the attached picture (my notepad file) in Matlab and change the value of there into a constant number(e.g. 4 for values greater than 0) and again write they (changed lines 2 to 44) into my notepad file instead of their previous values. I tried with below code:
x=4; M= dlmread('my text file name.dsp','',1,0); %% .dsp is my file extension that is readable with notepad. M(M>0)=x; dlmwrite('my text file name.dsp',M,'delimiter','\t','precision','%.6f','roffset',1); ................................................................................................................................................. After this, I used the command of "type my text file name.dsp " for displaying content of written text file. It displayed a matrix 44*10 in the command window (MATLAB). It was good, but when I opened my text file with Notepad, it has not a correct arrangement. I mean is that it was not in accordance with the main file (44rows*10columns). How can to fix this?
My another question is that when I read the lines of 2 to 44 my text file and write thire again. This approach did not allow the first line ( in main file) to be also written in the modified file. How be writing done so that the first line in the main file to be transferred without the change into the modified file (after writing)? Note that the first line contains several string and numeric characters. I attached my text file for completing my discription.
Thanks for any suggestion that whould be can help me.
The Best
Azade
  1 件のコメント
jonas
jonas 2018 年 7 月 19 日
Upload the text file instead of an image. Makes it easier to help you.

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

採用された回答

jonas
jonas 2018 年 7 月 19 日
Getting the table-format in the text is fixed easily by adding the arguments
...,'newline','pc')
Have not been able to preserve the first row.
  1 件のコメント
Azade Jamshidi
Azade Jamshidi 2018 年 7 月 19 日
It worked, thank you.
But it is essential to be preserved the first line for the next processes in my work. If you may have found out about this, please share your experience with me. It will be very appreciated.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by