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:
how to add a newline symbol into a string?

Subject: how to add a newline symbol into a string?

From: workaholic

Date: 20 Jan, 2010 14:04:45

Message: 1 of 6

I want obtain a string
 'abc
def'
But I don't know how to include a newline character into strings. how
can I do that?

Thanks in advance!

Subject: how to add a newline symbol into a string?

From: Steven Lord

Date: 20 Jan, 2010 14:12:05

Message: 2 of 6


"workaholic" <liuauto@gmail.com> wrote in message
news:0ff032ca-772e-449d-a9e3-746bf0d432e0@m16g2000yqc.googlegroups.com...
>I want obtain a string
> 'abc
> def'
> But I don't know how to include a newline character into strings. how
> can I do that?
>
> Thanks in advance!

S = sprintf('abc\ndef')

Note that S will NOT be a 2-by-3 char array but will be a 1-by-7 with the
4th character being the newline. If you're looking for a 2-by-3 char array,
use:

S = ['abc'; 'def']

or, if you need to worry about strings of different lengths, I'd use a cell
array.

S = {'abc'; 'def'; 'ghijklm'}
isequal(S{1} , 'abc') % true

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: how to add a newline symbol into a string?

From: kinor

Date: 20 Jan, 2010 14:14:04

Message: 3 of 6

workaholic <liuauto@gmail.com> wrote in message <0ff032ca-772e-449d-a9e3-746bf0d432e0@m16g2000yqc.googlegroups.com>...
> I want obtain a string
> 'abc
> def'
> But I don't know how to include a newline character into strings. how
> can I do that?
>
> Thanks in advance!

str = sprintf('test \n test')

or

str = ['test ' char(10) ' 'test']

hth
kinor

Subject: how to add a newline symbol into a string?

From: Matt J

Date: 20 Jan, 2010 15:14:03

Message: 4 of 6

"kinor " <kinor.removethiswithdot@gmx.de> wrote in message <hj733c$n79$1@fred.mathworks.com>...

>
> str = sprintf('test \n test')
>
> or
>
> str = ['test ' char(10) ' 'test']
>

From a previous post by Yair Altman:

"
Note that char(10) is equivalent to \n only on some systems: Macs use char(13), Unix use char(10) and Windows uses both char(10) and char(13)... Therefore, using '\n' is more platform-independent. An alternative is to use double(java.lang.System.getProperty('line.separator').char) which returns the current platform's definition (remove the cast to double() if you just need the newline string).

BTW, you don't need char() when concatenating: ['a' char(10) 'b'] is the same as ['a' 10 'b']
"

Subject: how to add a newline symbol into a string?

From: Jan Simon

Date: 20 Jan, 2010 21:25:20

Message: 5 of 6

Dear Matt!

> From a previous post by Yair Altman:
> "
> Note that char(10) is equivalent to \n only on some systems: Macs use char(13), Unix use char(10) and Windows uses both char(10) and char(13)... Therefore, using '\n' is more platform-independent. An alternative is to use double(java.lang.System.getProperty('line.separator').char) which returns the current platform's definition (remove the cast to double() if you just need the newline string). "

This is sloppy.
1. "Macs" mean MacOS 9, while current Macs rum OS-X, which uses char(10).
2. \n is char(10) ever. char(13) is \r ever. Both are independent from the platform. Try it on Windows or Linux/Mac:
  double(sprintf('a\nb')) => 97 10 98
3. The differences between Windows and Unix appear when you open a file in text-mode:
  FID = fopen('Test.ascii', 'wt');
  fprintf(FID, 'a\n');
  fclose(FID):
This writes ['a' char(10)] on unix and ['a', char([13, 10])] on Windows.
But:
  FID = fopen('Test.ascii', 'wb');
  fprintf(FID, 'a\n');
  fclose(FID):
writes ['a' char(10)] ever.

Matlab's command window creates one line break for char(10), char(13) and char([13,10]), and 2 line breaks for char([10,13]).

Conclusion: \n is indepent from the platform, FOPEN(File, 't') is not.

Kind regards, Jan

Subject: how to add a newline symbol into a string?

From: workaholic

Date: 21 Jan, 2010 03:38:29

Message: 6 of 6

On Jan 20, 10:12 pm, "Steven Lord" <sl...@mathworks.com> wrote:
> "workaholic" <liua...@gmail.com> wrote in message
>
> news:0ff032ca-772e-449d-a9e3-746bf0d432e0@m16g2000yqc.googlegroups.com...
>
> >I want obtain a string
> > 'abc
> > def'
> > But I don't know how to include a newline character into strings. how
> > can I do that?
>
> > Thanks in advance!
>
> S = sprintf('abc\ndef')
>
> Note that S will NOT be a 2-by-3 char array but will be a 1-by-7 with the
> 4th character being the newline.  If you're looking for a 2-by-3 char array,
> use:
>
> S = ['abc'; 'def']
>
> or, if you need to worry about strings of different lengths, I'd use a cell
> array.
>
> S = {'abc'; 'def'; 'ghijklm'}
> isequal(S{1} , 'abc') % true
>
> --
> Steve Lord
> sl...@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ:http://matlabwiki.mathworks.com/MATLAB_FAQ

Thanks a lot!

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