fprintf and line break

Hi,
I'm trying to write an array into a text file using fprintf. It's working fine on my Mac Matlab Version 2015b. However, on my work PC with a 2011 version, it is not breaking the line, but writing everything into one single line. Since I only have those two setups, it's hard for me to figure out if it is a Mac/PC or a version issue. Also, maybe I'm missing something in the syntax?
filetimenormal=fullfile(savepath,'file.txt');
ftnormal=fopen(filetimenormal, 'w');
header={'year','mm', 'dd', 'hh', 'min', 'sec', 'file', 'type'};
fprintf(ftnormal,'%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n',header{1,:});
[nrows,ncols] = size(newtimearray);
for row = 1:nrows
fprintf(ftnormal,'%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n' ,newtimearray{row,:});
end
Thank you!

3 comentarios

Geoff Hayes
Geoff Hayes el 30 de Ag. de 2016
Anke - is this true for the header too? Is it on the same line as the data? Put a breakpoint at the second fprintf and check to see whether the newtimearray{row,:} data types make sense for the output format string.
Anke Kügler
Anke Kügler el 30 de Ag. de 2016
Geoff, yes, even the header is on the same like as the rest of the lines
Stephen23
Stephen23 el 30 de Ag. de 2016
@Anke Kügler : the solution is to read the fopen documentation, and add the t option to the permissions string. Walter Roberson's answer is the correct solution.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 30 de Ag. de 2016

1 voto

For MS Windows, use
ftnormal=fopen(filetimenormal, 'wt');
instead of
ftnormal=fopen(filetimenormal, 'w');
Image Analyst
Image Analyst el 30 de Ag. de 2016

0 votos

Depending on the program (notepad, wordpad, MSWord, etc.) the new lines are interpreted differently. If \n is not working, try \n\r or \r\n. One of those should work.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Ag. de 2016

Comentada:

el 30 de Ag. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by