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:
fprintf printing long strings

Subject: fprintf printing long strings

From: CyberFrog

Date: 20 Jun, 2010 22:56:05

Message: 1 of 6

Hi all,

I have a simple fprintf code as follows:

fido = fopen(myfile, 'a');
fprintf(fido,['Name: ',mystring,'\n']);

where mystring is: 'dogs' and this seems t print ok. However if mystring is longer i.e. I have tried to use a pathname instead,

mystring='C:/all/this/way/to/get/home/sweet/home'

I get a warning:

Warning: Invalid escape sequence appears in format string. See help
sprintf for valid escape sequences.

Does anyone know what this means?

Subject: fprintf printing long strings

From: dpb

Date: 20 Jun, 2010 23:31:06

Message: 2 of 6

CyberFrog wrote:
> Hi all,
>
> I have a simple fprintf code as follows:
>
> fido = fopen(myfile, 'a');
> fprintf(fido,['Name: ',mystring,'\n']);
>
> where mystring is: 'dogs' and this seems t print ok. However if
> mystring is longer i.e. I have tried to use a pathname instead,
>
> mystring='C:/all/this/way/to/get/home/sweet/home'
>
> I get a warning:
>
> Warning: Invalid escape sequence appears in format string. See help
> sprintf for valid escape sequences.
> Does anyone know what this means?

Yeah, you tried to put in the \n character w/o a formatting string--

Try

fprintf(fido,'%s\n',['Name: ',mystring]);

instead...

--

Subject: fprintf printing long strings

From: us

Date: 21 Jun, 2010 00:16:07

Message: 3 of 6

"CyberFrog" <domlee55@hotmail.com> wrote in message <hvm6a5$70k$1@fred.mathworks.com>...
> Hi all,
>
> I have a simple fprintf code as follows:
>
> fido = fopen(myfile, 'a');
> fprintf(fido,['Name: ',mystring,'\n']);
>
> where mystring is: 'dogs' and this seems t print ok. However if mystring is longer i.e. I have tried to use a pathname instead,
>
> mystring='C:/all/this/way/to/get/home/sweet/home'
>
> I get a warning:
>
> Warning: Invalid escape sequence appears in format string. See help
> sprintf for valid escape sequences.
>
> Does anyone know what this means?

very strange...
as thist works flawlessly and as expected...

     s='C:/all/this/way/to/get/home/sweet/home';
     sprintf(['Name: ',s,'\n'])
% ans = Name: C:/all/this/way/to/get/home/sweet/home

us

Subject: fprintf printing long strings

From: dpb

Date: 21 Jun, 2010 01:08:42

Message: 4 of 6

us wrote:
> "CyberFrog" <domlee55@hotmail.com> wrote in message
> <hvm6a5$70k$1@fred.mathworks.com>...
>> Hi all,
>>
>> I have a simple fprintf code as follows:
>>
>> fido = fopen(myfile, 'a');
>> fprintf(fido,['Name: ',mystring,'\n']);
>>
>> where mystring is: 'dogs' and this seems t print ok. However if
>> mystring is longer i.e. I have tried to use a pathname instead,
>>
>> mystring='C:/all/this/way/to/get/home/sweet/home'
>>
>> I get a warning:
>>
>> Warning: Invalid escape sequence appears in format string. See help
>> sprintf for valid escape sequences.
>> Does anyone know what this means?
>
> very strange...
> as thist works flawlessly and as expected...
>
> s='C:/all/this/way/to/get/home/sweet/home';
> sprintf(['Name: ',s,'\n'])
> % ans = Name: C:/all/this/way/to/get/home/sweet/home
>

W/ old R12 the \n w/o a format specification complains that a variable n
is undefined...

I don't know nuthink' (a la Sgt Schultzie :) ) hardly about the C
formatting so had never thought of not needing a format specification
but I guess I can see since "ordinary" text is just passed thru that it
might oughta' should work...

--
--

Subject: fprintf printing long strings

From: Doug Schwarz

Date: 21 Jun, 2010 16:18:23

Message: 5 of 6

CyberFrog wrote:
> Hi all,
>
> I have a simple fprintf code as follows:
>
> fido = fopen(myfile, 'a');
> fprintf(fido,['Name: ',mystring,'\n']);
>
> where mystring is: 'dogs' and this seems t print ok. However if
> mystring is longer i.e. I have tried to use a pathname instead,
>
> mystring='C:/all/this/way/to/get/home/sweet/home'
>
> I get a warning:
>
> Warning: Invalid escape sequence appears in format string. See help
> sprintf for valid escape sequences.
> Does anyone know what this means?

If you really used forward slashes then this should be fine, but if you
actually used backslashes (like are normally used on Windows) then you
have constructed a format string containing things like '\w' and that is
an invalid escape sequence. Obviously, your example is not what you
really used so I can't be more specific. The problem is not the length
of mystring.

The way to avoid the problem is

   fprintf(fido,'Name: %s\n',mystring);

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: fprintf printing long strings

From: CyberFrog

Date: 21 Jun, 2010 16:34:24

Message: 6 of 6

Doug Schwarz <see@sig.for.address.edu> wrote in message <fhMTn.83673$HG1.64233@newsfe21.iad>...
> CyberFrog wrote:
> > Hi all,
> >
> > I have a simple fprintf code as follows:
> >
> > fido = fopen(myfile, 'a');
> > fprintf(fido,['Name: ',mystring,'\n']);
> >
> > where mystring is: 'dogs' and this seems t print ok. However if
> > mystring is longer i.e. I have tried to use a pathname instead,
> >
> > mystring='C:/all/this/way/to/get/home/sweet/home'
> >
> > I get a warning:
> >
> > Warning: Invalid escape sequence appears in format string. See help
> > sprintf for valid escape sequences.
> > Does anyone know what this means?
>
> If you really used forward slashes then this should be fine, but if you
> actually used backslashes (like are normally used on Windows) then you
> have constructed a format string containing things like '\w' and that is
> an invalid escape sequence. Obviously, your example is not what you
> really used so I can't be more specific. The problem is not the length
> of mystring.
>
> The way to avoid the problem is
>
> fprintf(fido,'Name: %s\n',mystring);
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.


Many thanks all, in fact the first reply got my mystring to pass through successfully!

Thanks again for your invaluable input

Tags for 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