Got Questions? Get Answers.
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:
create a cell of strings from numbers

Subject: create a cell of strings from numbers

From: T

Date: 4 Aug, 2009 07:04:01

Message: 1 of 5

I need a cell of strings. In this case, the strings are created from numbers.

I use this code to create what I want:
jj = 0;
for ii = 0:200:2000
jj = jj+1;
    labels{jj} = num2str(ii);
end

I'm sure there must be a way to do this with just a line of code (instead of a loop), using some combination of num2cell, cellstr, sprintf or whatever. I just can't figure it out.

Subject: create a cell of strings from numbers

From: us

Date: 4 Aug, 2009 07:24:01

Message: 2 of 5

"T " <tDO.Tdamsma@studentPO.INTtudelftPERI.ODnl> wrote in message <h58mh1$nsu$1@fred.mathworks.com>...
> I need a cell of strings. In this case, the strings are created from numbers.
>
> I use this code to create what I want:
> jj = 0;
> for ii = 0:200:2000
> jj = jj+1;
> labels{jj} = num2str(ii);
> end
>
> I'm sure there must be a way to do this with just a line of code (instead of a loop), using some combination of num2cell, cellstr, sprintf or whatever. I just can't figure it out.

one of the solutions

     ix=0:10:50;
     r=arrayfun(@(x) sprintf('%d',x),ix,'uni',false).'
%{
% r =
          '0'
          '10'
          '20'
          '30'
          '40'
          '50'
%}

us

Subject: create a cell of strings from numbers

From: Camille David

Date: 4 Aug, 2009 07:55:19

Message: 3 of 5

"us " <us@neurol.unizh.ch> wrote in message <h58nmh$m4u$1@fred.mathworks.com>...
> "T " <tDO.Tdamsma@studentPO.INTtudelftPERI.ODnl> wrote in message <h58mh1$nsu$1@fred.mathworks.com>...
> > I need a cell of strings. In this case, the strings are created from numbers.
> >
> > I use this code to create what I want:
> > jj = 0;
> > for ii = 0:200:2000
> > jj = jj+1;
> > labels{jj} = num2str(ii);
> > end
> >
> > I'm sure there must be a way to do this with just a line of code (instead of a loop), using some combination of num2cell, cellstr, sprintf or whatever. I just can't figure it out.
>
> one of the solutions
>
> ix=0:10:50;
> r=arrayfun(@(x) sprintf('%d',x),ix,'uni',false).'
> %{
> % r =
> '0'
> '10'
> '20'
> '30'
> '40'
> '50'
> %}
>
> us


Another solution:
labels=cellstr(num2str((0:200:2000)'));

Subject: create a cell of strings from numbers

From: us

Date: 4 Aug, 2009 10:43:03

Message: 4 of 5

"Camille David"
> Another solution:
> labels=cellstr(num2str((0:200:2000)'));

just two thoughts to an otherwise good other solution...

NUM2STR comes with a big overhead:

     type num2str;

CELLSTR calls DEBLANK, which may lead to undesirable side-effects, eg, concatenation later in the program...

us

Subject: create a cell of strings from numbers

From: T

Date: 4 Aug, 2009 10:58:01

Message: 5 of 5

"us " <us@neurol.unizh.ch> wrote in message <h593bn$lr2$1@fred.mathworks.com>...
> "Camille David"
> > Another solution:
> > labels=cellstr(num2str((0:200:2000)'));
>
> just two thoughts to an otherwise good other solution...
>
> NUM2STR comes with a big overhead:
>
> type num2str;
>
> CELLSTR calls DEBLANK, which may lead to undesirable side-effects, eg, concatenation later in the program...
>
> us

Well, you solved my problem. I had tried labels=cellstr(num2str((0:200:2000)')) myself, but then without the '
I'm aware of the overhead of num2str, but for my case that is not a problem.

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