find the size of a structured array

87 ビュー (過去 30 日間)
Richard
Richard 2012 年 1 月 9 日
編集済み: Stephen23 2022 年 12 月 13 日
I have a structured array called 'Data' which is a 1x1 struct, within this structured array are Data.note1, Data.note2, Data.note3, Data.note4. How is it possible to find the number of fields in the structured array? Where if it was a cell you would use length(cell)! Doing this for the structured array returns 1 where it should be 4.
  1 件のコメント
Stephen23
Stephen23 2022 年 12 月 13 日
編集済み: Stephen23 2022 年 12 月 13 日
"Doing this for the structured array returns 1 where it should be 4."
No, it should return 1 because you have a scalar structure, you even wrote this yourself: "I have a structured array called 'Data' which is a 1x1 struct". A 1x1 array is scalar, i.e. has only one element, regardless of its class:
In contrast a structure array with four elements has ... four elements:
S = struct('X',{1,2,3,4}) % 1x4, not 1x1
S = 1×4 struct array with fields:
X
length(S)
ans = 4
The size of a structure array is completely independent of how many fields it has.

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

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2012 年 1 月 9 日
length(fieldnames(Data))

その他の回答 (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