load change write text files in matlab

4 views (last 30 days)
Kamuran
Kamuran on 12 Jan 2016
Answered: Walter Roberson on 12 Jan 2016
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 Comment
Kamuran
Kamuran on 12 Jan 2016
Edited: Kamuran on 12 Jan 2016
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 Jan 2016
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);

Community Treasure Hunt

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

Start Hunting!