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:
Combing Cell Matrix into a Cell Array

Subject: Combing Cell Matrix into a Cell Array

From: Aaron

Date: 24 Apr, 2013 22:54:09

Message: 1 of 6

So I have a 3x3 cell matrix like the following.

A={'1' '2' 'b' ; '3' '4' 'c'; '5' '6' 'd'}
The output looks like this
A=
'1' '2' 'b'
'3' '4' 'c'
'5' '6' 'd'

Where each individual character is in its own cell. I want to combine each row of cells to make a cell array.

A=
'12b'
'34c'
'56d'

I know that if you have all numbers num2str will give what i want. Char just gives one long array. Any ideas.

Subject: Combing Cell Matrix into a Cell Array

From: Bruno Luong

Date: 24 Apr, 2013 23:40:11

Message: 2 of 6

 num2cell(cellfun(@char,A),2)

% Bruno

Subject: Combing Cell Matrix into a Cell Array

From: Aaron

Date: 25 Apr, 2013 17:09:10

Message: 3 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kl9qgr$lpi$1@newscl01ah.mathworks.com>...
> num2cell(cellfun(@char,A),2)
>
> % Bruno

Thanks for the response Bruno, that works well but apparently my example didn't capture my array properly, the cells are of different number of characters. So a more accurate representation would be.
A={'1' '2' 'ab'; '4' '5' 'cd; '7' '8' 'ef'}

A= '1' '2' 'ab'
     '4' '5' 'cd'
     '6 '7' 'ef'

When this is the case UniformOutput has to be set to false. The result gives a 3x1 cell array of 1x3 cells. I would like a 3x1 cell array as follows.
A={'1 2 ab'; '4 5 cd'; '6 7 ef'}

Subject: Combing Cell Matrix into a Cell Array

From: Bruno Luong

Date: 26 Apr, 2013 11:22:07

Message: 4 of 6

Try this:

A={'1' '2' 'ab'; '4' '5' 'cd'; '7' '8' 'ef'}
B = arrayfun(@(i) strtrim(sprintf('%s ', A{i,:})), (1:size(A,1))', 'unif', 0)

% Bruno

Subject: Combing Cell Matrix into a Cell Array

From: Aaron

Date: 26 Apr, 2013 18:21:09

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kldo0v$7vj$1@newscl01ah.mathworks.com>...
> Try this:
>
> A={'1' '2' 'ab'; '4' '5' 'cd'; '7' '8' 'ef'}
> B = arrayfun(@(i) strtrim(sprintf('%s ', A{i,:})), (1:size(A,1))', 'unif', 0)
>
> % Bruno

That's perfect Bruno. Thanks for your help.
-Aaron

Subject: Combing Cell Matrix into a Cell Array

From: Richie

Date: 27 Apr, 2013 15:08:08

Message: 6 of 6

"Aaron" wrote in message <klegil$qck$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kldo0v$7vj$1@newscl01ah.mathworks.com>...
> > Try this:
> >
> > A={'1' '2' 'ab'; '4' '5' 'cd'; '7' '8' 'ef'}
> > B = arrayfun(@(i) strtrim(sprintf('%s ', A{i,:})), (1:size(A,1))', 'unif', 0)
> >
> > % Bruno
>
> That's perfect Bruno. Thanks for your help.
> -Aaron

Thanks guys this really helped!



[url=http://www.familyplanningclinic.ie]Family Planning Clinic [/url]

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