Why won't \n give me a new line?

419 Ansichten (letzte 30 Tage)
Katie
Katie am 7 Aug. 2013
Kommentiert: Walter Roberson am 23 Aug. 2022
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 Kommentare
Priya Mittal
Priya Mittal am 4 Sep. 2019
Change the 'w' parameter in fopen to 'wt'. Worked for me.
Jan
Jan am 4 Sep. 2019
Fortunately even NotePad shipped with modern Windows 10 is able to display \n correctly now.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 8 Aug. 2013
It works perfectly when you write it to a file also. But the Windows NotePad ("Editor" in German) fails to display the linebreaks correctly. But Matlab's editor, Word, NotePad++, WordPad, (X)Emacs (under Linux and Windows), Alpha (on MacOS-9 !), BBEdit, vi, etc. display the \n line break correctly even without the \r.
When you open the file in the 'wt' mode, \n is converted to \r\n automatically, but this can have certain unexpected side effects. E.g. the number of characters obtained by ftell can differ from the number of bytes the file has. Reading by fgets will be slower, because all control characters are considered on the fly, e.g. backspaces and ^Z as end-of-file. In addition the created results differ between Windows and Linux. So I'd prefer to retire NotePad, install NotePad++ or WordPad as standard viewer of txt files and prefer the \n linebreaks as Matlab's editor does for several years now.
  1 Kommentar
Evangelos Stefanidis
Evangelos Stefanidis am 6 Mai 2019
Thank you Jan, I had totally forgot about Notepad++ ! Also Thank you Katie for asking this question, I had hard time for a while and I was only opening the .txt files with my results through matlab... lol.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Cedric
Cedric am 7 Aug. 2013
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 Kommentare
Keqin Xu
Keqin Xu am 11 Jan. 2019
Files created this way cannot be properly read using ...'deliminator','\n'...
An example is attached.
Walter Roberson
Walter Roberson am 11 Jan. 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 .)

Melden Sie sich an, um zu kommentieren.


Camilo Rocha
Camilo Rocha am 25 Apr. 2019
You just have to put 'wt' when you call fopen function
fid1 = fopen(filename,'wt')

Roxanne de la Garza
Roxanne de la Garza am 3 Feb. 2018
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 Kommentare
Matthes Müller
Matthes Müller am 7 Feb. 2018
Worked for me also, thank you so much!
ustin yanu
ustin yanu am 6 Apr. 2020
Bearbeitet: ustin yanu am 6 Apr. 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')

Melden Sie sich an, um zu kommentieren.


Aasim
Aasim am 13 Nov. 2013
Works well.. Thanks!

Milad Hasani
Milad Hasani am 22 Aug. 2022
As a suggestion, you can use:
\newline
  1 Kommentar
Walter Roberson
Walter Roberson am 23 Aug. 2022
\newline is for Latex, and will not work for fprintf() or sprintf()
fprintf('abc\newlinedef')
abc ewlinedef

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by