Why won't \n give me a new line?
Mostrar comentarios más antiguos
I am trying to write my data to a text file but the usual \n is not giving me a new line!
fid1 = fopen(filedesignation,'w');
fprintf(fid1,'%s, ',title1);
fprintf(fid1,'%4.2f, %4.2f, %4.2f, %4.2f, %4.2f\n',DataMatrix);
fclose(fid1);
When I try printing this to the command window, the \n works, but not when I try and write to a file. Why?!
How can I get a line return in my text file if this doesn't work?
6 comentarios
Stephen23
el 7 de Feb. de 2018
The simplest solution is to open the file in text mode:
fid1 = fopen(filedesignation,'wt');
^ you need this!
On Windows this will automatically convert all \n to the required newlines characters.
Mohammad Bhat
el 25 de Feb. de 2018
That helped me too, thanks ...
Shahrokh Abbasi-Rad
el 7 de Sept. de 2018
Awesome. the problem was only a 't' :))))
Fernando Rojano
el 6 de Abr. de 2019
Great!
Priya Mittal
el 4 de Sept. de 2019
Change the 'w' parameter in fopen to 'wt'. Worked for me.
Jan
el 4 de Sept. de 2019
Fortunately even NotePad shipped with modern Windows 10 is able to display \n correctly now.
Respuesta aceptada
Más respuestas (5)
Cedric
el 7 de Ag. de 2013
10 votos
You will need \r\n for producing a carriage return and a new line. This is a well documented issue between UNIX-based and Windows operating systems.
3 comentarios
Yang Hu
el 30 de Nov. de 2018
Brilliant
Keqin Xu
el 11 de En. de 2019
Files created this way cannot be properly read using ...'deliminator','\n'...
An example is attached.
Walter Roberson
el 11 de En. de 2019
the success is going to depend on which command you are passing that parameter to and potentially on how you open the file.
Generally telling a function that the delimiter is \n is an interface contract: you are making a promise about the file and the function is permitted to take you at your word.
My guess looking at that file is that you would use
parts = regexp( fileread('Run_01.txt'), '\r?\n', 'split');
if isempty(parts{end})
parts(end) = [];
end
that will work whether the file has cr lf or lf only. It will not work for cr only though. This code will not remove interior empty lines. The if at the bottom is to handle the fact that if the file ends in newline then the split process thinks that there is an empty line after it. Files are permitted to either just end without a newline or else to end in a newline (that is to say that there is no standard as to whether newline is a line separator or else a line terminator .)
Camilo Rocha
el 25 de Abr. de 2019
You just have to put 'wt' when you call fopen function
fid1 = fopen(filename,'wt')
Roxanne de la Garza
el 3 de Feb. de 2018
1 voto
I put my \n at the beginning instead of the end and it worked for me. fprintf(fid1,'\n %4.2f, %4.2f, %4.2f, %4.2f, %4.2f',DataMatrix);
2 comentarios
Matthes Müller
el 7 de Feb. de 2018
Worked for me also, thank you so much!
ustin yanu
el 6 de Abr. de 2020
Editada: ustin yanu
el 6 de Abr. de 2020
worked for me too, thx, wonder why?
i have this
elseif x<1300
a=x-1200;
b = [ fix(a/1e+2)-1E+2*fix(a/1e+4) rem(a, 1e+2)];
fprintf('\nYou entered %d:%d%d',b)
fprintf('\nThe equivalent time based on a 12-hour clock is %d:%d%d',b)
disp('PM')
Aasim
el 13 de Nov. de 2013
0 votos
Works well.. Thanks!
Milad Hasani
el 22 de Ag. de 2022
As a suggestion, you can use:
\newline
1 comentario
\newline is for Latex, and will not work for fprintf() or sprintf()
fprintf('abc\newlinedef')
Categorías
Más información sobre Environment and Settings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!