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:
Cell Array 'Header Row'

Subject: Cell Array 'Header Row'

From: Eric W

Date: 12 Mar, 2009 20:58:01

Message: 1 of 3

I have a simple cell array, equivalent to {'A' 'B' 'C' 'D' ... 'Z'} and I'd like to add a row to that cell array numbering the columns:

{ 1 2 3 4 ... 26;
 'A' 'B' 'C' 'D' ... 'Z' }

The length of the array is not known beforehand. Is there any simple way of doing this? I keep trying various combinations of parenthesis and brackets on the following lines:

raw = {'test' 'foo' 'bar' 'zoot'}; %Fake input
headers = raw(1,:);
headers{2,:} = 1:length(headers);

but I end up with <<??? The right hand side of this assignment has too few values to satisfy the left hand side.>> or each cell containing the 1:length(headers) array.

Subject: Cell Array 'Header Row'

From: Jos

Date: 12 Mar, 2009 21:06:01

Message: 2 of 3

"Eric W" <gmail@ewilliams2006.com> wrote in message <gpbt0p$8kj$1@fred.mathworks.com>...
> I have a simple cell array, equivalent to {'A' 'B' 'C' 'D' ... 'Z'} and I'd like to add a row to that cell array numbering the columns:
>
> { 1 2 3 4 ... 26;
> 'A' 'B' 'C' 'D' ... 'Z' }
>
> The length of the array is not known beforehand. Is there any simple way of doing this? I keep trying various combinations of parenthesis and brackets on the following lines:
>
> raw = {'test' 'foo' 'bar' 'zoot'}; %Fake input
> headers = raw(1,:);
> headers{2,:} = 1:length(headers);
>
> but I end up with <<??? The right hand side of this assignment has too few values to satisfy the left hand side.>> or each cell containing the 1:length(headers) array.

Is this what you want?

raw = {'test' 'foo' 'bar' 'zoot'}; %Fake input
header = num2cell(1:numel(raw))
final = [header ; raw]
% [ 1] [ 2] [ 3] [ 4]
% 'test' 'foo' 'bar' 'zoot'

hth
Jos

Subject: Cell Array 'Header Row'

From: Eric W

Date: 12 Mar, 2009 21:19:01

Message: 3 of 3

"Jos " <#10584@fileexchange.com> wrote in message <gpbtfo$bka$1@fred.mathworks.com>...
> "Eric W" <gmail@ewilliams2006.com> wrote in message <gpbt0p$8kj$1@fred.mathworks.com>...
> > I have a simple cell array, equivalent to {'A' 'B' 'C' 'D' ... 'Z'} and I'd like to add a row to that cell array numbering the columns:
> >
> > { 1 2 3 4 ... 26;
> > 'A' 'B' 'C' 'D' ... 'Z' }
> >
> > The length of the array is not known beforehand. Is there any simple way of doing this? I keep trying various combinations of parenthesis and brackets on the following lines:
> >
> > raw = {'test' 'foo' 'bar' 'zoot'}; %Fake input
> > headers = raw(1,:);
> > headers{2,:} = 1:length(headers);
> >
> > but I end up with <<??? The right hand side of this assignment has too few values to satisfy the left hand side.>> or each cell containing the 1:length(headers) array.
>
> Is this what you want?
>
> raw = {'test' 'foo' 'bar' 'zoot'}; %Fake input
> header = num2cell(1:numel(raw))
> final = [header ; raw]
> % [ 1] [ 2] [ 3] [ 4]
> % 'test' 'foo' 'bar' 'zoot'
>
> hth
> Jos

That's exactly what I was looking for. I used mat2cell, but that caused the correct array to be replicated in each cell. Thanks a lot!

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