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.
load change write text files in matlab
2 次查看(过去 30 天)
显示 更早的评论
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 个)
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!