Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Unable to get new line fprintf

Subject: Unable to get new line fprintf

From: Ravin

Date: 15 Mar, 2013 17:44:09

Message: 1 of 8

Hi i have written a code to write data into a text file:

fileID = fopen('testWrite.txt','w');
mystring = 'aX';
fprintf(fileID,'%s\n',['Name: ',mystring]);
fprintf(fileID,'%d',21);

I get the result:

Name: aX21

instead of

Name: aX
21

Can anyone explain this to me?

Thanks in advance

Subject: Unable to get new line fprintf

From: dpb

Date: 15 Mar, 2013 17:59:46

Message: 2 of 8

On 3/15/2013 12:44 PM, Ravin wrote:
> Hi i have written a code to write data into a text file:
>
> fileID = fopen('testWrite.txt','w'); mystring = 'aX';
> fprintf(fileID,'%s\n',['Name: ',mystring]); fprintf(fileID,'%d',21);
>
> I get the result:
> Name: aX21
>
> instead of
> Name: aX
> 21
> Can anyone explain this to me?
...

As a guess the lack of the 't' on the fopen in 'wt' maybe resulted in
the \n not being correct for the display software/editor used to look
at/open the resulting file.

Try

fileID = fopen('testWrite.txt','wt');

and let us know if that doesn't fix the problem.

--

Subject: Unable to get new line fprintf

From: Steven_Lord

Date: 15 Mar, 2013 18:42:55

Message: 3 of 8



"Ravin " <ravin2288@yahoo.com> wrote in message
news:khvml9$j9l$1@newscl01ah.mathworks.com...
> Hi i have written a code to write data into a text file:
>
> fileID = fopen('testWrite.txt','w');
> mystring = 'aX';
> fprintf(fileID,'%s\n',['Name: ',mystring]);
> fprintf(fileID,'%d',21);
>
> I get the result:
>
> Name: aX21
>
> instead of
>
> Name: aX
> 21
>
> Can anyone explain this to me?

If you're using Windows, open the file in the MATLAB Editor or in Wordpad
instead of opening it in Notepad.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Unable to get new line fprintf

From: anyone

Date: 15 Mar, 2013 18:50:22

Message: 4 of 8

On Fri, 15 Mar 2013 17:44:09 +0000, Ravin wrote:

> Hi i have written a code to write data into a text file:
>
> fileID = fopen('testWrite.txt','w');

opens a local file for writing, in binary mode.

> mystring = 'aX';
> fprintf(fileID,'%s\n',['Name: ',mystring]); fprintf(fileID,'%d',21);

could also do:

fprintf(fileID,'Name: %s\n',mystring);
fprintf(fileID,'%d',21);

p.s., remember to add:

fclose(fileID);
 
> Thanks in advance

for what?

Subject: Unable to get new line fprintf

From: dpb

Date: 15 Mar, 2013 20:10:27

Message: 5 of 8

On 3/15/2013 1:42 PM, Steven_Lord wrote:
...

> If you're using Windows, open the file in the MATLAB Editor or in
> Wordpad instead of opening it in Notepad.

But isn't it still better to ensure the correct newline character(s) for
the OS (that is, the 'wt' option which can't hurt, anyway)?

I have some other app's besides Notepad that are also sensitive altho
granted most may never know the difference...

--

Subject: Unable to get new line fprintf

From: Stan24

Date: 17 Mar, 2013 10:47:06

Message: 6 of 8

dpb <none@non.net> wrote in message <khvv78$veb$1@speranza.aioe.org>...
> On 3/15/2013 1:42 PM, Steven_Lord wrote:
> ...
>
> > If you're using Windows, open the file in the MATLAB Editor or in
> > Wordpad instead of opening it in Notepad.
>
> But isn't it still better to ensure the correct newline character(s) for
> the OS (that is, the 'wt' option which can't hurt, anyway)?
>
> I have some other app's besides Notepad that are also sensitive altho
> granted most may never know the difference...
>
> --
>
>

Operating systems usually represent a newline with one or two control characters:
LF - Unix(Linux) and CR+LF for Windows.
fid = fopen('testWrite.txt','w');
mystring = 'aX';
fprintf(fid,'Name: %s\r\n',mystring);
fprintf(fid,'%d',21);
fclose(fid);

Subject: Unable to get new line fprintf

From: dpb

Date: 17 Mar, 2013 13:46:45

Message: 7 of 8

On 3/17/2013 5:47 AM, Stan24 wrote:
> dpb <none@non.net> wrote in message <khvv78$veb$1@speranza.aioe.org>...
>> On 3/15/2013 1:42 PM, Steven_Lord wrote:
>> ...
>>
>> > If you're using Windows, open the file in the MATLAB Editor or in
>> > Wordpad instead of opening it in Notepad.
>>
>> But isn't it still better to ensure the correct newline character(s)
>> for the OS (that is, the 'wt' option which can't hurt, anyway)?
>>
>> I have some other app's besides Notepad that are also sensitive altho
>> granted most may never know the difference...
...

>
> Operating systems usually represent a newline with one or two control
> characters:
> LF - Unix(Linux) and CR+LF for Windows.
> fid = fopen('testWrite.txt','w');
> mystring = 'aX';
> fprintf(fid,'Name: %s\r\n',mystring);
> fprintf(fid,'%d',21);
> fclose(fid);

And it's the (missing) 't' on the fopen() in Matlab that ensures that is
done transparently for the OS (basically WIndows since it's the one w/
the two-character lf sequence) is/was the point...

> help fopen
> ...
> In text mode on the PC, the carriage return character preceding
> a newline character is deleted on input and added before the newline
> character on output. To open in text mode, add 't' to the
> permission string, for example 'rt' and 'wt+'. (On Unix, text and
> binary mode are the same so this has no effect. But on PC systems
> this is critical.)
> ...

...

--

Subject: Unable to get new line fprintf

From: Horia

Date: 25 Aug, 2014 17:15:10

Message: 8 of 8

"Ravin" wrote in message <khvml9$j9l$1@newscl01ah.mathworks.com>...
> Hi i have written a code to write data into a text file:
>
> fileID = fopen('testWrite.txt','w');
> mystring = 'aX';
> fprintf(fileID,'%s\n',['Name: ',mystring]);
> fprintf(fileID,'%d',21);
>
> I get the result:
>
> Name: aX21
>
> instead of
>
> Name: aX
> 21
>
> Can anyone explain this to me?
>
> Thanks in advance


<<You could add this line in front of your fprintf:
<<fprintf(fid,'\n');

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us