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:
convert matrix (double) to cell array (string) without for loop

Subject: convert matrix (double) to cell array (string) without for loop

From: Stefan Siegel

Date: 26 Sep, 2007 10:13:39

Message: 1 of 17

Could you please help me to convert a matrix of doulbes into
a cell array of strings without using a for loop?

example:

a = [1, 2, 3];
... -> b = {'1', '2', '3'}

(I only found posts about the other way round.)

Subject: convert matrix (double) to cell array (string) without for loop

From: Stefan Siegel

Date: 26 Sep, 2007 10:53:41

Message: 2 of 17

Please answer in _this_ thread and not in the other "double
post thread".

Sorry for the inconvenience.

Subject: convert matrix (double) to cell array (string) without for loop

From: Jos

Date: 26 Sep, 2007 11:19:50

Message: 3 of 17

"Stefan Siegel" <stefan.siegel@mailinator.com> wrote in
message <fddbcj$sc5$1@fred.mathworks.com>...
> Could you please help me to convert a matrix of doulbes
into
> a cell array of strings without using a for loop?
>
> example:
>
> a = [1, 2, 3];
> ... -> b = {'1', '2', '3'}
>
> (I only found posts about the other way round.)


a = 1:3 ;
b = strread(num2str(a),'%s')

hth
Jos

Subject: convert matrix (double) to cell array (string) without for loop

From: Loren Shure

Date: 26 Sep, 2007 11:34:32

Message: 4 of 17

In article <fddbcj$sc5$1@fred.mathworks.com>,
stefan.siegel@mailinator.com says...
> Could you please help me to convert a matrix of doulbes into
> a cell array of strings without using a for loop?
>
> example:
>
> a = [1, 2, 3];
> ... -> b = {'1', '2', '3'}
>
> (I only found posts about the other way round.)
>

mat2cell(a,1,ones(1,size(a,2)))

--
Loren
http://blogs.mathworks.com/loren/

Subject: convert matrix (double) to cell array (string) without for loop

From: Doug Schwarz

Date: 26 Sep, 2007 13:08:34

Message: 5 of 17

In article <fddbcj$sc5$1@fred.mathworks.com>,
 "Stefan Siegel" <stefan.siegel@mailinator.com> wrote:

> Could you please help me to convert a matrix of doulbes into
> a cell array of strings without using a for loop?
>
> example:
>
> a = [1, 2, 3];
> ... -> b = {'1', '2', '3'}
>
> (I only found posts about the other way round.)


num2cell

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

Subject: convert matrix (double) to cell array (string) without for loop

From: Jos

Date: 26 Sep, 2007 15:59:57

Message: 6 of 17

Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-D4C548.09084026092007@news.frontiernet.net>...
> In article <fddbcj$sc5$1@fred.mathworks.com>,
> "Stefan Siegel" <stefan.siegel@mailinator.com> wrote:
>
> > Could you please help me to convert a matrix of doulbes
into
> > a cell array of strings without using a for loop?
> >
> > example:
> >
> > a = [1, 2, 3];
> > ... -> b = {'1', '2', '3'}
> >
> > (I only found posts about the other way round.)
>
>
> num2cell
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Note that the OP requested a cell array of strings. Both
num2cell and mat2cell do not do that ...

Jos

Subject: convert matrix (double) to cell array (string) without for loop

From: Doug Schwarz

Date: 26 Sep, 2007 18:31:43

Message: 7 of 17

In article <fddvlt$7o0$1@fred.mathworks.com>,
 "Jos " <DELjos@jasenDEL.nl> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-D4C548.09084026092007@news.frontiernet.net>...
> > In article <fddbcj$sc5$1@fred.mathworks.com>,
> > "Stefan Siegel" <stefan.siegel@mailinator.com> wrote:
> >
> > > Could you please help me to convert a matrix of doulbes
> into
> > > a cell array of strings without using a for loop?
> > >
> > > example:
> > >
> > > a = [1, 2, 3];
> > > ... -> b = {'1', '2', '3'}
> > >
> > > (I only found posts about the other way round.)
> >
> >
> > num2cell
> >
>
> Note that the OP requested a cell array of strings. Both
> num2cell and mat2cell do not do that ...
>
> Jos

Oops, you're right. Sorry for causing any confusion.

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

Subject: convert matrix (double) to cell array (string) without for loop

From: Miguel

Date: 21 Feb, 2010 18:57:04

Message: 8 of 17

"Jos " <DELjos@jasenDEL.nl> wrote in message <fddvlt$7o0$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-D4C548.09084026092007@news.frontiernet.net>...
> > In article <fddbcj$sc5$1@fred.mathworks.com>,
> > "Stefan Siegel" <stefan.siegel@mailinator.com> wrote:
> >
> > > Could you please help me to convert a matrix of doulbes
> into
> > > a cell array of strings without using a for loop?
> > >
> > > example:
> > >
> > > a = [1, 2, 3];
> > > ... -> b = {'1', '2', '3'}
> > >
> > > (I only found posts about the other way round.)
> >
> >
> > num2cell
> >
> > --
> > Doug Schwarz
> > dmschwarz&ieee,org
> > Make obvious changes to get real email address.
>
> Note that the OP requested a cell array of strings. Both
> num2cell and mat2cell do not do that ...
>
> Jos

Hello,

I´d like to convert an array of doubles to a cell array of strings in order of using it as a default answer of inputdlg(), but the former method didn´t work:

" ??? Error using ==>
inputdlg at 242
Default Answer must be a
cell array of strings. "

If anyone could help me out, i´d appreciate a lot.
Thank you,

Miguel

Subject: convert matrix (double) to cell array (string) without for loop

From: Alex B

Date: 27 May, 2011 12:46:04

Message: 9 of 17

cellstr(num2str(a'))'

Subject: convert matrix (double) to cell array (string) without for loop

From: Todd Welti

Date: 13 Oct, 2011 02:05:30

Message: 10 of 17

"Alex B" wrote in message <iro6ec$ee4$1@newscl01ah.mathworks.com>...
> cellstr(num2str(a'))'

NONE of the methods in this thread worked for me. I'm writing my own looping funciton.

Subject: convert matrix (double) to cell array (string) without for loop

From: TideMan

Date: 13 Oct, 2011 02:22:40

Message: 11 of 17

On Oct 13, 3:05 pm, "Todd Welti" <twe...@harman.com> wrote:
> "Alex B" wrote in message <iro6ec$ee...@newscl01ah.mathworks.com>...
> > cellstr(num2str(a'))'
>
> NONE of the methods in this thread worked for me.  I'm writing my own looping funciton.

That's your prerogative, of course, and there's nothing wrong with
loops.
Though vectorised code is more elegant.

Tell us why Alex B's method did not work for you.
I'm wondering if your problem is to do with whether a is a row or a
column vector?
If you modify the code slightly, it doesn't matter:
cellstr(num2str(a(:)))

Subject: convert matrix (double) to cell array (string) without for loop

From: Kelvin

Date: 15 Oct, 2011 20:23:05

Message: 12 of 17

> Tell us why Alex B's method did not work for you.
> I'm wondering if your problem is to do with whether a is a row or a
> column vector?
> If you modify the code slightly, it doesn't matter:
> cellstr(num2str(a(:)))

I'm having the same problem, with the purpose of concatenating a
cellstring and an array of double for file output.

Although Stefan Siegal gave an example of a 1D array, the original
question was for a more general matrix, such as 2D.

for example:
a=[1 2 3 ; 4 5 6 ; 7 8 9];
b=cellstr(num2str(a(:)));

does not produce the same as
b={ '1' '2' '3' ; '4' '5' '6' ; '7' '8' '9' }

Subject: convert matrix (double) to cell array (string) without for loop

From: Bruno Luong

Date: 16 Oct, 2011 07:31:30

Message: 13 of 17

Kelvin <pasquinade@hotmail.com> wrote in message <e369abfa-7e3f-44a1-8caf-14f69e104c1c@w5g2000yqw.googlegroups.com>...
>
> for example:
> a=[1 2 3 ; 4 5 6 ; 7 8 9];
> b=cellstr(num2str(a(:)));
>
> does not produce the same as
> b={ '1' '2' '3' ; '4' '5' '6' ; '7' '8' '9' }

b = arrayfun(@num2str, a, 'unif', 0)

this compact code will be as slow as a for-loop.

Bruno

Subject: convert matrix (double) to cell array (string) without for loop

From: Richard Crozier

Date: 28 Jun, 2012 14:35:08

Message: 14 of 17

Kelvin <pasquinade@hotmail.com> wrote in message <e369abfa-7e3f-44a1-8caf-14f69e104c1c@w5g2000yqw.googlegroups.com>...
> > Tell us why Alex B's method did not work for you.
> > I'm wondering if your problem is to do with whether a is a row or a
> > column vector?
> > If you modify the code slightly, it doesn't matter:
> > cellstr(num2str(a(:)))
>
> I'm having the same problem, with the purpose of concatenating a
> cellstring and an array of double for file output.
>
> Although Stefan Siegal gave an example of a 1D array, the original
> question was for a more general matrix, such as 2D.
>
> for example:
> a=[1 2 3 ; 4 5 6 ; 7 8 9];
> b=cellstr(num2str(a(:)));
>
> does not produce the same as
> b={ '1' '2' '3' ; '4' '5' '6' ; '7' '8' '9' }

I don't know how fast it is, but you can do

b = reshape(cellstr(num2str(a(:))), size(a))

to get what you want then.

Subject: convert matrix (double) to cell array (string) without for loop

From: Sebastian Holmqvist

Date: 3 Jul, 2012 19:39:07

Message: 15 of 17

"Stefan Siegel" wrote in message <fddbcj$sc5$1@fred.mathworks.com>...
> Could you please help me to convert a matrix of doulbes into
> a cell array of strings without using a for loop?
>
> example:
>
> a = [1, 2, 3];
> ... -> b = {'1', '2', '3'}
>
> (I only found posts about the other way round.)
a = [1:3; 4:6];
cellfun(@num2str, num2cell(a), 'UniformOutput', false)

ans =
    '1' '2' '3'
    '4' '5' '6'

Is that what you're looking for?

Subject: convert matrix (double) to cell array (string) without for loop

From: Siddhartha

Date: 19 Nov, 2012 20:03:22

Message: 16 of 17

"Sebastian Holmqvist" <sebastian.holmqvist@gmail.com> wrote in message <jsvhor$l9o$1@newscl01ah.mathworks.com>...
> "Stefan Siegel" wrote in message <fddbcj$sc5$1@fred.mathworks.com>...
> > Could you please help me to convert a matrix of doulbes into
> > a cell array of strings without using a for loop?
> >
> > example:
> >
> > a = [1, 2, 3];
> > ... -> b = {'1', '2', '3'}
> >
> > (I only found posts about the other way round.)
> a = [1:3; 4:6];
> cellfun(@num2str, num2cell(a), 'UniformOutput', false)
>
> ans =
> '1' '2' '3'
> '4' '5' '6'
>
> Is that what you're looking for?

Yes! Thanks Sebastian - this does exactly what OP and I wanted.

Subject: convert matrix (double) to cell array (string) without for loop

From: Bruno Luong

Date: 19 Nov, 2012 21:02:18

Message: 17 of 17

> cellfun(@num2str, num2cell(a), 'UniformOutput', false)

NUM2CELL/CELLFUN are not neceassary,. For example this will do:

a = [1 2 3]
arrayfun(@num2str, a, 'unif', 0)

% Bruno

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