fprintf does not print a new line

1 vue (au cours des 30 derniers jours)
Maria
Maria le 9 Jan 2015
I am working in windows 8, 64 bits, with matlab R12b
I need to export data that contains several lines of script and numerical information, The problem is that fprintf does not recognize the carrige return command and what I get in the output file is a single row that concatenates all the rows I and exporting.
I've been reading at Matlab central and it seams to be not solved bug. I've tried all the variants I've found: a) to use
fid = fopen('pruebafprintf.txt','wt');
  • insted of
fid = fopen('pruebafprintf.txt','w');
b) use \r\n or \n or \n\r in
fprintf(fid, '%s %d %g %g %d %d %d %g %g\n\r', [char(paciente) num2str(compilado(row,:))]);
but didn't solve the problem. Here is a code example:
compilado = [231 0.00523810000000000 0.278930000000000 1 1 0 0 0;
252 0.00571430000000000 0.865600000000000 2 2 2 0.00560000000000000 0.856400000000000;
271 0.00614510000000000 0.898010000000000 4 4 0 0 0;
306 0.00693880000000000 0.815730000000000 5 5 5 0.00680000000000000 0.844400000000000];
paciente = 'AD17'
fid = fopen('pruebafprintf.txt','wt');
[rows,cols]=size(compilado);
for row=1:rows,
fprintf(fid, '%s %d %g %g %d %d %d %g %g\n\r', [char(paciente) num2str(compilado(row,:))]);
end
fclose(fid);
How can I solve this problem? I 've to export a large amount of data. Thanks Maria Eugenia

Réponses (2)

Image Analyst
Image Analyst le 9 Jan 2015
How are you displaying/retrieving the text? Different programs handle the \n and \r differently, like MSWord, notepad, wordpad, etc. I use fprintf() all the time and it works. Maybe you need to use an editor that can display the characters in hex so you can see if the 10 and 13 are actually being written out to the file. If you're getting them, then it's not a problem with fprintf(), but a problem with how you're displaying the text.

Star Strider
Star Strider le 9 Jan 2015
Modifié(e) : Star Strider le 9 Jan 2015
I see the problem. Note that you are specifying a series of numeric field descriptors, then writing string variables to them:
fprintf(fid, '%s %d %g %g %d %d %d %g %g\n\r', [char(paciente) num2str(compilado(row,:))]);
See if changing to something like this works:
fprintf(fid, '%s %d %g %g %d %d %d %g %g\n\r', paciente, compilado(row,:));

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by