How is to read and write a text file in matlab?
2 次查看(过去 30 天)
显示 更早的评论
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!