Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fprintf not printing new line

I am trying to send an array that is [2 x N] doubles large to a text file using the fprintf() command. I am having problems in that fprintf() is not recognizing the new line command (\n) or the carriage return command (\r). The code I am using is

fid = fopen([Image.Dir,'CtlPts_',Image.Files{k},'.txt'],'w');
fprintf(fid,'%.4f\t%.4f\n',control_points{k});
fclose(fid);

where the data I am trying to print is in the cell control_points{k}.

The tab gets printed fine, but everything in the text file gets printed on one line, so this is why I am assuming that it is ignoring my new line character.

Is there something wrong with my syntax that I am not seeing?

like image 380
thron of three Avatar asked Jun 30 '11 14:06

thron of three


2 Answers

I know that on many systems, \n is not enough to create what you're asking for (and so, maybe you have to do \r\n)

like image 193
KevinDTimm Avatar answered Oct 06 '22 00:10

KevinDTimm


An alternative solution is to open the file in text mode, that way MATLAB automatically inserts a carriage return \r before any newline \n character in the output on Windows systems:

fid = fopen('file.txt', 'wt');
fprintf(fid, '%f\t%f\n', rand(10,2));
fclose(fid);

Note that this is somewhat unnecessary, since most editors (with the exception of Microsoft Notepad) recognize Unix/Mac/Windows line endings.

like image 30
Amro Avatar answered Oct 06 '22 00:10

Amro