load change write text files in matlab

조회 수: 2 (최근 30일)
Kamuran
Kamuran 2016년 1월 12일
답변: Walter Roberson 2016년 1월 12일
Hello, I have a text file which I need to open and make some changes. It is consists of characters and numbers. I want do change some characters and numbers and save as .text file again. My file is TFdat.txt (20000,1) with numbers and characters. And I have no problem loading it.
M=fileread('TFdat.txt');
M(1)=t;
M(5)=5;
.
.
.
and I want to save as .txt file again. But dlmwrite, fwrite , save do not work. numbers are integers from 0 to 9 and can be saved as strings.
any idea how to do this?
Thank you
  댓글 수: 1
Kamuran
Kamuran 2016년 1월 12일
편집: Kamuran 2016년 1월 12일
well I could save the file as excel file. That is not really what I wanted because if the user does not have excel that is a problem for me. So still I need some help.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 12일
When you use fileread() you get back a character vector. When you write into that character vector you need to be sure that you are either writing characters or the numeric codes that are equivalent to the characters you want to write. For example, you wrote the numeric value 5, which is the numeric code for the non-printing special character named Enquire (ENQ); if you wanted the digit 5 to go into that position you would have to assign '5' or assign 53 (which is the numeric code for the character '5')
Once you have written the desired characters into your M, you can fwrite to a file.
For example,
M = fileread('TFdat.txt');
M(1) = 't';
M(5) = '5';
fid = fopen('newTFdata.txt', 'w');
fwrite(fid, M);
fclose(fid);

카테고리

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