Access value in cell arrays

A{1,1}.str = 1;
A{2,1}.str = 2;
... (so on)
A{10,1}.str = 10;
Can I say:
B = A{:,1}.str;
so that:
B=[1 2 3 4 5 6 7 8 9 10];
Thanks very much

2 件のコメント

per isakson
per isakson 2013 年 6 月 29 日
Is A supposed to be a cell arrays of structures?
A field named "str" holding a numerical value isn't that confusing?
TN
TN 2013 年 6 月 29 日
Yes... A is a cell arrays. Each cell array is a structure.
'str' is just a name. It does not mean to be a string. Sorry for the confusion.

サインインしてコメントする。

 採用された回答

per isakson
per isakson 2013 年 6 月 29 日
編集済み: per isakson 2013 年 6 月 29 日

3 投票

What you look for can be achieved with cellfun, see below.
Indexing like C{:}.field is not supported (AFAIK).
The script
S1.field=1;
S2.field=2;
S3.field=3;
C = { S1, S2, S3 };
C{1}.field
C{2}.field
C{3}.field
C{:}.field
returns
ans =
1
ans =
2
ans =
3
Bad cell reference operation.
And
vec = cellfun( @(S) S.field, C, 'uni', true )
returns
ans =
1 2 3
.
EDIT
The single command with cellfun is justified in case the cells of the cell array contain structures with only some fields in common.
Example:
S1.field=1;
S2.field=2;
S3.field=3;
S1.field1=1;
S2.field2=2;
S3.field3=3;
C = { S1, S2, S3 };
vec = cellfun( @(S) S.field, C, 'uni', true )
returns
vec =
1 2 3

4 件のコメント

TN
TN 2013 年 6 月 29 日
Thanks for your response. I am looking for a single command like the way Matlab handling the normal matrix array instead of using the "for" loop.
Matt J
Matt J 2013 年 6 月 29 日
per gave you a single command solution
vec = cellfun( @(S) S.field, C, 'uni', true )
TN
TN 2013 年 6 月 29 日
Thanks very much, Per and Matt. That's it. Sorry... somehow i didn't see the few last lines from "Per". You are all genious!
Matt J
Matt J 2013 年 6 月 29 日
You can Accept-click per's Answer, then, since that was what you were looking for.

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2013 年 6 月 29 日
編集済み: Matt J 2013 年 6 月 29 日

1 投票

No. You can do this instead
A(1).str = 1;
A(2).str = 2;
...
A(10).str = 10;
B=[A(:).str]

3 件のコメント

TN
TN 2013 年 6 月 29 日
Hi Matt, Thanks for your response... but my problem is when A is a cell "{}" array (not a matrix "()" array).
Matt J
Matt J 2013 年 6 月 29 日
It would not make sense to hold structures having the same fields inside cells. It just makes them harder to get to (as you've discovered).
per isakson
per isakson 2013 年 6 月 29 日
編集済み: per isakson 2013 年 6 月 29 日
I agree.
However, for some reason the cell array may contain structures with only some fields in common.

サインインしてコメントする。

James Tursa
James Tursa 2013 年 6 月 29 日

1 投票

Another variation:
x = [A{:,1}];
B = [x.str];

2 件のコメント

TN
TN 2013 年 6 月 29 日
Yes... thanks very much, James. Your solution works great for me too!
Matt J
Matt J 2013 年 6 月 30 日
TN, if James solution works for you, there is really no reason to be carrying around A. You may as well just use x. As per said, it might make sense if A{i} were structs with different fields, but James' approach will not work if that is the case.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

タグ

質問済み:

TN
2013 年 6 月 29 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by