Thread Subject: Writing to Cells

Subject: Writing to Cells

From: james bejon

Date: 16 Mar, 2010 19:22:23

Message: 1 of 7

Dear All,

I'm trying to alter the contents of an array of cells. I thought that something like

a = {2:5}'
b = {3:6}'
a{2:end, 1} = b{2:end, 1}

would accomplish such a task, but it doesn't. Can someone please explain where I'm going wrong?

Subject: Writing to Cells

From: us

Date: 16 Mar, 2010 19:40:22

Message: 2 of 7

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <hnolpf$4uq$1@fred.mathworks.com>...
> Dear All,
>
> I'm trying to alter the contents of an array of cells. I thought that something like
>
> a = {2:5}'
> b = {3:6}'
> a{2:end, 1} = b{2:end, 1}
>
> would accomplish such a task, but it doesn't. Can someone please explain where I'm going wrong?

one of the solutions

     a={(2:5).'}; % <- note proper use of TRANSPOSE...
     b={(3:6).'};
     a{1}(2:end,1)=b{1}(2:end,1);
     a{1}
%{
% ans =
     2
     4
     5
     6
%}

us

Subject: Writing to Cells

From: james bejon

Date: 16 Mar, 2010 21:50:26

Message: 3 of 7

I see. Thanks. By the way, why is this use of transpose 'proper'? I've seen other people use it, but it seemed a bit counter-intuitive to me.

Subject: Writing to Cells

From: us

Date: 16 Mar, 2010 22:07:05

Message: 4 of 7

"james bejon" <jamesbejon@yahoo.co.uk> wrote in message <hnouf2$jdc$1@fred.mathworks.com>...
> I see. Thanks. By the way, why is this use of transpose 'proper'? I've seen other people use it, but it seemed a bit counter-intuitive to me.

well... let's see...

% since you wrote
     a={2:5}'
% the assumption was that you wanted to create a CELL, which contains a COL vec
% above syntax does NOT achieve this... as a matter of fact, it does NOT do
% anything because the transpose of a 1x1 CELL is... a 1x1 CELL
     [size(a);size(a.')]
%{
% ans =
     1 1
     1 1
%}
% and
     a{:}
% ans = 2 3 4 5 % <- still a (default) ROW vec
% now,
     a={(2:5).'};
     a{:}
%{
% ans = % <- a COL vec
     2
     3
     4
     5
%}

just a thought...
us

Subject: Writing to Cells

From: james bejon

Date: 16 Mar, 2010 22:39:06

Message: 5 of 7

Thanks. I'm still struggling a bit with this though. Suppose I've imported some text data from an Excel spreadsheet. It's come in as, say, a 20 x 10 cell. Now, how do I copy, say, the 5th column to the 4th column, or everything from the 3rd cell down in the 5th column to their equivalents in the 4th column? I'm obviously not understanding the syntax here.

Subject: Writing to Cells

From: Steven Lord

Date: 17 Mar, 2010 14:30:45

Message: 6 of 7


"james bejon" <jamesbejon@yahoo.co.uk> wrote in message
news:hnp1a9$sqh$1@fred.mathworks.com...
> Thanks. I'm still struggling a bit with this though. Suppose I've
> imported some text data from an Excel spreadsheet. It's come in as, say,
> a 20 x 10 cell. Now, how do I copy, say, the 5th column to the 4th
> column, or everything from the 3rd cell down in the 5th column to their
> equivalents in the 4th column? I'm obviously not understanding the syntax
> here.

Indexing into a cell array with parentheses () operates on the cells in the
cell array.
Indexing into a cell array with curly braces {} operates on _the contents
of_ the cells in the cell array.

C = cell(5, 3);% using a smaller example for demonstration
for k = 1:numel(C)
    C{k} = k; % Since this uses {}, we're assigning to the _contents_ of the
kth cell
    C(k) = {k}; % or we can assign a cell as the kth cell using ()
end
C % Just for display purposes

C(:, 2) = C(:, 3) % copying the CELLS from the 3rd column into the CELLS in
the 2nd column
% or
[C{:, 1}] = C{:, 3} % copy the _contents_ of the cells from the 3rd column
into the _contents_ of the cells in the 1st column

The latter example may take you a little bit of time to understand, and I
think we only made it work without DEAL a few (4 or 5) releases ago. For an
explanation, look at the following page from the documentation that talks
about comma-separated lists:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br2js35-1.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Writing to Cells

From: james bejon

Date: 17 Mar, 2010 22:05:22

Message: 7 of 7

Thanks Steve. That's a really helpful response.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
transpose us 16 Mar, 2010 18:09:10
cell us 16 Mar, 2010 15:44:19
syntax us 16 Mar, 2010 15:44:19
code us 16 Mar, 2010 15:44:19
writing james bejon 16 Mar, 2010 15:24:09
altering james bejon 16 Mar, 2010 15:24:09
arrays james bejon 16 Mar, 2010 15:24:09
cells james bejon 16 Mar, 2010 15:24:09
rssFeed for this Thread

Contact us at files@mathworks.com