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

조회 수: 3 (최근 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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by