Accessing Fields in Structs

1 回表示 (過去 30 日間)
MiauMiau
MiauMiau 2014 年 3 月 14 日
コメント済み: MiauMiau 2014 年 3 月 14 日
Hi
I have a more theoretical question to struct arrays in Matlab. Given I have two structures a and b, defined as follows:
a = struct('b', 0, 'c', 'test')
a(2).b = 5
b = struct('b', {0,5}, 'c', {'test'})
WHY is it now that if I use a(2) the answer is simply
b: 5
c: []
i.e. the second element of c is shown as empty, whereas when I use b(2) the answer is
b: 5
c: 'test'
i.e. the first element of c is repeated as its second element, too?

採用された回答

Walter Roberson
Walter Roberson 2014 年 3 月 14 日
Look at the documentation for struct():
If any of the value inputs are nonscalar cell arrays, then s has the same dimensions as the nonscalar cell arrays. For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s.
Notice the second sentence there. {'test'} is a scalar cell array so it gets copied to all elements of the output.
  1 件のコメント
MiauMiau
MiauMiau 2014 年 3 月 14 日
Thanks, well that was a bit cryptic before :) But still one question: 'test' is obviously a string - so why is it then called a scalar cell array (and not a nonscalar cell array)?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by