Convert string to txt

I have a 4x1 string array, how can i write it and save it as a txt file? Not sure if i did it correctly.
A=(4x1) string array
fid = fopen('A.txt','wt');
fprintf(fid, '%s\n',A);
fclose(fid);

5 comentarios

Rik
Rik el 2 de Abr. de 2020
Can't you open the resulting file with notepad (or even the Matlab editor) to check the output? I would expect this code to work as intended. Why do you think there is a problem?
Jose Grimaldo
Jose Grimaldo el 2 de Abr. de 2020
Editada: Jose Grimaldo el 2 de Abr. de 2020
I ran it and it outputs this. I dont know why it outputs a 5. How can i fix that?
1 A
2 B
3 C
4 D
5
Ameer Hamza
Ameer Hamza el 2 de Abr. de 2020
What is the value matrix A?
Jose Grimaldo
Jose Grimaldo el 2 de Abr. de 2020
nwm i figure it out. Once i fclose is it consider save as txt file or do i have to inset at the end after fclose save('A.txt') ?
Ameer Hamza
Ameer Hamza el 2 de Abr. de 2020
Editada: Ameer Hamza el 5 de Abr. de 2020
You should not run save('A.txt'). It is useless for your problem.

Iniciar sesión para comentar.

Respuestas (1)

Maadhav Akula
Maadhav Akula el 5 de Abr. de 2020

0 votos

Hi,
I think you are getting that extra line because of the '\n' escape character. You can try the following code :
fid = fopen('A.txt','w');
fprintf(fid, '%s\n',A(1:end-1));
fprintf(fid,'%s',A(end));
fclose(fid);
Hope this Helps!

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Abr. de 2020

Editada:

el 5 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by