load change write text files in matlab

2 visualizaciones (últimos 30 días)
Kamuran
Kamuran el 12 de En. de 2016
Respondida: Walter Roberson el 12 de En. de 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 comentario
Kamuran
Kamuran el 12 de En. de 2016
Editada: Kamuran el 12 de En. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de En. de 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);

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by