Thread Subject: cell arrays and structs

Subject: cell arrays and structs

From: David

Date: 9 Aug, 2009 10:38:58

Message: 1 of 9

I have a 380x1 cell array fPN. Each cell has class 'char' and contains
a string.
I also have a 380x1 cell array ta. Each cell has class 'struct'. For
example I get

>> ta{5}

ans =

        num: '1'
       cond: 'CANCER'
         vf: '1'
    protein: 'CEA_01'

My question is: Is there an array method to add to each ta{i} a new
field whose value will be the string in fPN{i}, or should I just give
up and loop through the cells, adding the new field one by one?

Thanks
David

Subject: cell arrays and structs

From: Oleg Komarov

Date: 9 Aug, 2009 11:09:02

Message: 2 of 9

David <david.b.a.epstein@googlemail.com> wrote in message <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>...
> I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> a string.
> I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> example I get
>
> >> ta{5}
>
> ans =
>
> num: '1'
> cond: 'CANCER'
> vf: '1'
> protein: 'CEA_01'
>
> My question is: Is there an array method to add to each ta{i} a new
> field whose value will be the string in fPN{i}, or should I just give
> up and loop through the cells, adding the new field one by one?
>
> Thanks
> David

All i can think of is:

% building my example
fPN = cellstr(['example1';'example2']);
ta{1} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein', 'CEA_01');
ta{2} = struct('num', 1, 'cond', 'CANCER', 'vf', 1, 'protein', 'CEA_01');

% solution 1
Temp = cellfun(@(x) struct2cell(x), ta, 'un',0);
Temp = [Temp{:}]; Temp(end+1,:) = fPN;
ta = cell2struct(Temp, {'num', 'cond', 'vf', 'protein', 'newfield'});

in this case "ta" becomes a 2x1 struct (not a cell anymore).

Otherways u can do a simple loop:
% solution 2
for i = 1:length(ta)
    ta{i}.newfield = fPN{i};
end

Subject: cell arrays and structs

From: Doug Schwarz

Date: 9 Aug, 2009 12:40:24

Message: 3 of 9

In article
<a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>,
 David <david.b.a.epstein@googlemail.com> wrote:

> I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> a string.
> I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> example I get
>
> >> ta{5}
>
> ans =
>
> num: '1'
> cond: 'CANCER'
> vf: '1'
> protein: 'CEA_01'
>
> My question is: Is there an array method to add to each ta{i} a new
> field whose value will be the string in fPN{i}, or should I just give
> up and loop through the cells, adding the new field one by one?
>
> Thanks
> David

Yes, suppose you want to use the field name 'string', then

  [ta.string] = fPN{:};

will do it if your version of MATLAB is new enough.

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

Subject: cell arrays and structs

From: Matt

Date: 9 Aug, 2009 15:20:02

Message: 4 of 9

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-82000A.08402409082009@news.frontiernet.net>...

> Yes, suppose you want to use the field name 'string', then
>
> [ta.string] = fPN{:};
>
> will do it if your version of MATLAB is new enough.
>

Of if not, then the following should work for older versions

[ta(1:length(fpN)).string] = deal(fPN{:});

Subject: cell arrays and structs

From: us

Date: 9 Aug, 2009 16:38:01

Message: 5 of 9

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-82000A.08402409082009@news.frontiernet.net>...
> In article
> <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>,
> David <david.b.a.epstein@googlemail.com> wrote:
> > I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> > a string.
> > I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> > example I get
> > >> ta{5}
> > ans =
> > num: '1'
> > My question is: Is there an array method to add to each ta{i} a new
> > field whose value will be the string in fPN{i}, or should I just give
> > up and loop through the cells, adding the new field one by one?

> Yes, suppose you want to use the field name 'string', then
> [ta.string] = fPN{:};
> will do it if your version of MATLAB is new enough.

no - does not work...
according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just STRUCTs...
hence, a simple for loop is probably the fastest solution...

us

Subject: cell arrays and structs

From: Matt

Date: 9 Aug, 2009 17:02:01

Message: 6 of 9

David <david.b.a.epstein@googlemail.com> wrote in message <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>...

> I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> example I get
>
> >> ta{5}
>
> ans =
>
> num: '1'
> cond: 'CANCER'
> vf: '1'
> protein: 'CEA_01'

This seems like a bad way to store data. Why not use a struct array

sta=[ta{:}];

instead of burying each struct inside a cell where it is harder to get at?

Subject: cell arrays and structs

From: Doug Schwarz

Date: 9 Aug, 2009 18:05:09

Message: 7 of 9

In article <h5mu19$lof$1@fred.mathworks.com>,
 "us " <us@neurol.unizh.ch> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-82000A.08402409082009@news.frontiernet.net>...
> > In article
> > <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>,
> > David <david.b.a.epstein@googlemail.com> wrote:
> > > I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> > > a string.
> > > I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> > > example I get
> > > >> ta{5}
> > > ans =
> > > num: '1'
> > > My question is: Is there an array method to add to each ta{i} a new
> > > field whose value will be the string in fPN{i}, or should I just give
> > > up and loop through the cells, adding the new field one by one?
>
> > Yes, suppose you want to use the field name 'string', then
> > [ta.string] = fPN{:};
> > will do it if your version of MATLAB is new enough.
>
> no - does not work...
> according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just
> STRUCTs...
> hence, a simple for loop is probably the fastest solution...
>
> us

You're right, Urs, I missed that. If each cell in ta is a struct with
the same fields they could be concatenated into a proper struct array
with

  ta2 = [ta{:}];

and then

  [ta2.string] = fPN{:};

would work. Then ta2 could be converted back to a cell array with

  c2 = num2cell(ta2);

if desired. But really it would be better to use ta2.

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

Subject: cell arrays and structs

From: Matt

Date: 9 Aug, 2009 19:02:01

Message: 8 of 9

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-846576.14050809082009@news.frontiernet.net>...
> In article <h5mu19$lof$1@fred.mathworks.com>,
> "us " <us@neurol.unizh.ch> wrote:
>
> > Doug Schwarz <see@sig.for.address.edu> wrote in message
> > <see-82000A.08402409082009@news.frontiernet.net>...
> > > In article
> > > <a2f1b899-3886-492b-adb0-9530210f6315@g19g2000vbi.googlegroups.com>,
> > > David <david.b.a.epstein@googlemail.com> wrote:
> > > > I have a 380x1 cell array fPN. Each cell has class 'char' and contains
> > > > a string.
> > > > I also have a 380x1 cell array ta. Each cell has class 'struct'. For
> > > > example I get
> > > > >> ta{5}
> > > > ans =
> > > > num: '1'
> > > > My question is: Is there an array method to add to each ta{i} a new
> > > > field whose value will be the string in fPN{i}, or should I just give
> > > > up and loop through the cells, adding the new field one by one?
> >
> > > Yes, suppose you want to use the field name 'string', then
> > > [ta.string] = fPN{:};
> > > will do it if your version of MATLAB is new enough.
> >
> > no - does not work...
> > according to the OP's spec, his/her TAs are CELLs of STRUCTs - and not just
> > STRUCTs...
> > hence, a simple for loop is probably the fastest solution...
> >
> > us
>
> You're right, Urs, I missed that. If each cell in ta is a struct with
> the same fields they could be concatenated into a proper struct array
> with
>
> ta2 = [ta{:}];
>
> and then
>
> [ta2.string] = fPN{:};
>
> would work. Then ta2 could be converted back to a cell array with
>
> c2 = num2cell(ta2);
>
> if desired. But really it would be better to use ta2.


Note that this solution doesn't really avoid for-loops. num2cell.m uses for loops internally...

Subject: cell arrays and structs

From: David

Date: 9 Aug, 2009 20:29:56

Message: 9 of 9

On 9 Aug, 18:02, "Matt " <x...@whatever.com> wrote:
> This seems like a bad way to store data. Why not use a struct array
>
> sta=[ta{:}];
>
> instead of burying each struct inside a cell where it is harder to get at?

The reason I have a cell array is that this is what an application of
regexp gave me. I was trying to find my way out of the cell array, but
didn't know how. Thanks everyone for showing me the construction---
this solves my problem.
David

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com