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 concatinate cell array with matrix

Subject: how to concatinate cell array with matrix

From: Nilesh Goel

Date: 31 May, 2011 03:10:04

Message: 1 of 9

Hi,

I want to concatenate a cell array of size 1X5 with the array containing numeric data of size 30X5 so that result will become 31X5. I want to append the cell array as the first row.
I had tried the cat command in several ways but could not get the result.

Thanks

Subject: how to concatinate cell array with matrix

From: Greg Heath

Date: 31 May, 2011 03:24:30

Message: 2 of 9

On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> Hi,
>
> I want to concatenate a cell array of size 1X5 with the array containing numeric data of size 30X5 so that result will become 31X5. I want to append the cell array as the first row.
> I had tried the cat command in several ways but could not get the result.
>
> Thanks

help cell2mat
doc cell2mat

Hope this helps.

Greg

Greg

Subject: how to concatinate cell array with matrix

From: Nilesh Goel

Date: 31 May, 2011 05:37:04

Message: 3 of 9

Hello Greg,

I dont get you, by converting cell array into matrix how it would help me.
Let me explain the issue again.

I have a cell array of 1X5, another array of double type of 37X5. Now I want a new matrix of 38X5which contains cell array of 1X5 as its first row and rest for another double array.

I had tried the cell2mat command in the following ways:-
cell2mat = xyz %xyz is the cell array name
then it outputs the elements f cell array as matrix elements while on the otherhane it flags an error when I use
b = cell2mat(xyz)

??? Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'cell'.

please help me out.
Thanks,

Greg Heath <heath@alumni.brown.edu> wrote in message <b3fa8309-91fd-4b8e-9c05-6f4038626c93@m40g2000vbt.googlegroups.com>...
> On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> > Hi,
> >
> > I want to concatenate a cell array of size 1X5 with the array containing numeric data of size 30X5 so that result will become 31X5. I want to append the cell array as the first row.
> > I had tried the cat command in several ways but could not get the result.
> >
> > Thanks
>
> help cell2mat
> doc cell2mat
>
> Hope this helps.
>
> Greg
>
> Greg

Subject: how to concatinate cell array with matrix

From: Steven_Lord

Date: 31 May, 2011 13:22:58

Message: 4 of 9



"Nilesh Goel" <nilesh.goel@gmail.com> wrote in message
news:is1uq0$csr$1@newscl01ah.mathworks.com...
> Hello Greg,
>
> I dont get you, by converting cell array into matrix how it would help me.
> Let me explain the issue again.
>
> I have a cell array of 1X5, another array of double type of 37X5. Now I
> want a new matrix of 38X5which contains cell array of 1X5 as its first row
> and rest for another double array.

This is not possible unless each element of your cell array is a cell
containing a scalar double. You could create a 38-by-5 cell array by
converting your double matrix into a 37-by-5 cell array and then perform the
concatenation with MAT2CELL, but I don't think that's what you're looking to
do.

What is your goal in performing this concatenation?

> I had tried the cell2mat command in the following ways:-
> cell2mat = xyz %xyz is the cell array name

This will prevent you from using the CELL2MAT function for as long as this
variable exists; if you create this variable in a function, it will prevent
you from using the CELL2MAT function inside that function.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: how to concatinate cell array with matrix

From: Nilesh Goel

Date: 31 May, 2011 13:50:19

Message: 5 of 9

Hi Steve,

My ultimate goal is to have a 38X5 cell array so that I can write this into a excel file using xlswrite command. and later read this excel to plot some graphs.

"Steven_Lord" <slord@mathworks.com> wrote in message <is2q3i$kct$1@newscl01ah.mathworks.com>...
>
>
> "Nilesh Goel" <nilesh.goel@gmail.com> wrote in message
> news:is1uq0$csr$1@newscl01ah.mathworks.com...
> > Hello Greg,
> >
> > I dont get you, by converting cell array into matrix how it would help me.
> > Let me explain the issue again.
> >
> > I have a cell array of 1X5, another array of double type of 37X5. Now I
> > want a new matrix of 38X5which contains cell array of 1X5 as its first row
> > and rest for another double array.
>
> This is not possible unless each element of your cell array is a cell
> containing a scalar double. You could create a 38-by-5 cell array by
> converting your double matrix into a 37-by-5 cell array and then perform the
> concatenation with MAT2CELL, but I don't think that's what you're looking to
> do.
>
> What is your goal in performing this concatenation?
>
> > I had tried the cell2mat command in the following ways:-
> > cell2mat = xyz %xyz is the cell array name
>
> This will prevent you from using the CELL2MAT function for as long as this
> variable exists; if you create this variable in a function, it will prevent
> you from using the CELL2MAT function inside that function.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: how to concatinate cell array with matrix

From: Greg Heath

Date: 31 May, 2011 14:27:03

Message: 6 of 9


CORRECTED FOR THE HEINOUS SIN OF TOP-POSTING!!

On May 31, 1:37 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> Greg Heath <he...@alumni.brown.edu> wrote in message <b3fa8309-91fd-4b8e-9c05-6f4038626...@m40g2000vbt.googlegroups.com>...
> > On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> > > Hi,
>
> > > I want to concatenate a cell array of size 1X5 with the array containing numeric data of size 30X5 so that result will become 31X5. I want to append the cell array as the first row.
> > > I had tried the cat command in several ways but could not get the result.
>
> > > Thanks
>
> > help cell2mat
> > doc cell2mat
>
> > Hope this helps.
>
> > Greg
>
> Hello Greg,
>
> I dont get you, by converting cell array into
> matrix how it would help me.

A = {1 2 3 4 5}
B = repmat([6 7 8 9 10],30,1);

C = [cell2mat(A);B]

Hope this helps.

Greg

Subject: how to concatinate cell array with matrix

From: Nilesh Goel

Date: 2 Jun, 2011 06:28:05

Message: 7 of 9

Greg Heath <heath@alumni.brown.edu> wrote in message <c30d8599-acff-414d-a91f-6049b4fe1f16@n10g2000vby.googlegroups.com>...
>
> CORRECTED FOR THE HEINOUS SIN OF TOP-POSTING!!
>
> On May 31, 1:37 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> > Greg Heath <he...@alumni.brown.edu> wrote in message <b3fa8309-91fd-4b8e-9c05-6f4038626...@m40g2000vbt.googlegroups.com>...
> > > On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> > > > Hi,
> >
> > > > I want to concatenate a cell array of size 1X5 with the array containing numeric data of size 30X5 so that result will become 31X5. I want to append the cell array as the first row.
> > > > I had tried the cat command in several ways but could not get the result.
> >
> > > > Thanks
> >
> > > help cell2mat
> > > doc cell2mat
> >
> > > Hope this helps.
> >
> > > Greg
> >
> > Hello Greg,
> >
> > I dont get you, by converting cell array into
> > matrix how it would help me.
>
> A = {1 2 3 4 5}
> B = repmat([6 7 8 9 10],30,1);
>
> C = [cell2mat(A);B]
>
> Hope this helps.
>
> Greg

Hello Greg,

The code you suggest me is working fine but does not serve my purpose. I have text data in my cell array A, and if I am using the following sequence of commands then it wont work:-

A = {'aaa' 'bbb' 'ccc' 'ddd' 'eee'};
B = rand(30,5); % this generates a 30X5 double array for me.
 %%Now when I type following command I observe an error:-
C = [cell2mat(A);B]

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.


What I guess command cell2mat(A) has the o/p:-
aaabbbcccdddeee

so cell2mat commmand is not useful to me. could you please suggest me some other alternative method.

Thanks

Subject: how to concatinate cell array with matrix

From: Greg Heath

Date: 4 Jun, 2011 01:49:15

Message: 8 of 9

% On Jun 2, 2:28 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
% > Greg Heath <he...@alumni.brown.edu> wrote in message
% <c30d8599-acff-414d-
a91f-6049b4fe1...@n10g2000vby.googlegroups.com>...
% >
% > > CORRECTED FOR THE HEINOUS SIN OF TOP-POSTING!!
% >
% > > On May 31, 1:37 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
% > > > Greg Heath <he...@alumni.brown.edu> wrote in message
%
<b3fa8309-91fd-4b8e-9c05-6f4038626...@m40g2000vbt.googlegroups.com>...
% > > > > On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com>
wrote:
% >
% > > > > > I want to concatenate a cell array of size 1X5 with the
% array containing numeric data of size 30X5 so that result will
become
% 31X5. I want to append the cell array as the first row.
% > > > > > I had tried the cat command in several ways but could not
% get the result.
% >
% > > > > help cell2mat
% > > > > doc cell2mat
% >
%-----SNIP
% > > > I dont get you, by converting cell array into
% > > > matrix how it would help me.
% >
% > > A = {1 2 3 4 5}
% > > B = repmat([6 7 8 9 10],30,1);
% >
% > > C = [cell2mat(A);B]
% >
%-----SNIP
% > The code you suggest me is working fine but does not serve my
% purpose. I have text data in my cell array A, and if I am using
% the following sequence of commands then it wont work:-
% >
% > A = {'aaa' 'bbb' 'ccc' 'ddd' 'eee'};
% > B = rand(30,5); % this generates a 30X5 double array for me.
% > %%Now when I type following command I observe an error:-
% > C = [cell2mat(A);B]
% >
% > ??? Error using ==> vertcat
% % > CAT arguments dimensions are not consistent.
% >
% > What I guess command cell2mat(A) has the o/p:-
% > aaabbbcccdddeee
% >
% > so cell2mat commmand is not useful to me. could you please
% suggest me some other alternative method.

clear all, clc

format short
randn('state',0)
B = randn(2,5)./randn(2,5)

% B =
%
% 2.3168 -0.2130 8.4055 1.1147 -3.4218
% -2.2949 0.1318 10.4529 -0.6348 -0.2098

sizeB = size(B) % [ 2 5 ]
matB = num2str(B)

% matB =
%
% 2.31679 -0.213035 8.40547 1.11474 -3.42183
% -2.29486 0.131769 10.4529 -0.634824 -0.209815

sizematB = size(matB) % [ 2 60 ]

disp(['NOTE: Although the printed spacing in B is uniform, ';...
      ' the spacing in matB is not! '])
disp('Spacing A accordingly ...')

A = { ' aaa ','bbb ','ccc ',...
        'ddd ','eee '}
sizeA = size(A) % [ 1 5 ]
matA = cell2mat(A)
sizematA = size(matA) % [ 1 60]

C = [ matA ; matB]

% C =
%
% aaa bbb ccc ddd
eee
% 2.31679 -0.213035 8.40547 1.11474 -3.42183
% -2.29486 0.131769 10.4529 -0.634824 -0.209815

end

disp('But why not make life easy?')

A = { 'aaa ','bbb ','ccc ','ddd ','eee '};
disp(A),disp(B)

% 'aaa ' 'bbb ' 'ccc ' 'ddd ' 'eee '
%
% 2.3168 -0.2130 8.4055 1.1147 -3.4218
% -2.2949 0.1318 10.4529 -0.6348 -0.2098

Hope this helps,

Greg

Subject: how to concatinate cell array with matrix

From: Greg Heath

Date: 4 Jun, 2011 01:54:08

Message: 9 of 9

On Jun 3, 9:49 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> % On Jun 2, 2:28 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> % > Greg Heath <he...@alumni.brown.edu> wrote in message
> % <c30d8599-acff-414d-
> a91f-6049b4fe1...@n10g2000vby.googlegroups.com>...
> % >
> % > > CORRECTED FOR THE HEINOUS SIN OF TOP-POSTING!!
> % >
> % > > On May 31, 1:37 am, "Nilesh Goel" <nilesh.g...@gmail.com> wrote:
> % > > > Greg Heath <he...@alumni.brown.edu> wrote in message
> %
> <b3fa8309-91fd-4b8e-9c05-6f4038626...@m40g2000vbt.googlegroups.com>...
> % > > > > On May 30, 11:10 pm, "Nilesh Goel" <nilesh.g...@gmail.com>
> wrote:
> % >
> % > > > > > I want to concatenate a cell array of size 1X5 with the
> % array containing numeric data of size 30X5 so that result will
> become
> % 31X5. I want to append the cell array as the first row.
> % > > > > > I had tried the cat command in several ways but could not
> % get the result.
> % >
> % > > > > help cell2mat
> % > > > > doc cell2mat
> % >
> %-----SNIP
> % > > > I dont get you, by converting cell array into
> % > > > matrix how it would help me.
> % >
> % > > A = {1 2 3 4 5}
> % > > B = repmat([6 7 8 9 10],30,1);
> % >
> % > > C = [cell2mat(A);B]
> % >
> %-----SNIP
> % > The code you suggest me is working fine but does not serve my
> % purpose. I have text data in my cell array A, and if I am using
> % the following sequence of commands then it wont work:-
> % >
> % > A = {'aaa' 'bbb' 'ccc' 'ddd' 'eee'};
> % > B = rand(30,5);      % this generates a 30X5 double array for me.
> % >  %%Now when I type following command I observe an error:-
> % > C = [cell2mat(A);B]
> % >
> % > ??? Error using ==> vertcat
> % % > CAT arguments dimensions are not consistent.
> % >
> % > What I guess command cell2mat(A) has the o/p:-
> % > aaabbbcccdddeee
> % >
> % > so cell2mat commmand is not useful to me. could you please
> % suggest me some other alternative method.
>
> clear all, clc
>
> format short
> randn('state',0)
> B        = randn(2,5)./randn(2,5)
>
> % B =
> %
> %     2.3168   -0.2130    8.4055    1.1147   -3.4218
> %    -2.2949    0.1318   10.4529   -0.6348   -0.2098
>
> sizeB    = size(B)    % [ 2 5 ]
> matB     = num2str(B)
>
> % matB =
> %
> %  2.31679    -0.213035      8.40547      1.11474     -3.42183
> % -2.29486     0.131769      10.4529    -0.634824    -0.209815
>
> sizematB = size(matB) % [ 2 60 ]
>
> disp(['NOTE: Although the printed spacing in B is uniform, ';...
>       '      the spacing in matB is not!                   '])
> disp('Spacing A accordingly ...')
>
> A = { ' aaa         ','bbb           ','ccc         ',...
>         'ddd          ','eee     '}
> sizeA    = size(A)     % [ 1 5 ]
> matA     = cell2mat(A)
> sizematA = size(matA)  % [ 1 60]
>
> C = [ matA ; matB]
>
> % C =
> %
> %  aaa           bbb               ccc           ddd
> eee
> %  2.31679    -0.213035      8.40547      1.11474     -3.42183
> % -2.29486     0.131769      10.4529    -0.634824    -0.209815
>
> end
>
> disp('But why not make life easy?')
>
> A = { 'aaa ','bbb ','ccc ','ddd ','eee '};
> disp(A),disp(B)
>
> %     'aaa '       'bbb '      'ccc '      'ddd '       'eee '
> %
> %     2.3168   -0.2130    8.4055    1.1147   -3.4218
> %    -2.2949    0.1318   10.4529   -0.6348   -0.2098
>
> Hope this helps,
>
> Greg

If you cut and paste the code in the command window,
the alignment should be correct. I don't know how to
preserve that spacing when posting.

Greg

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