フィルターのクリア

How to create struct of cell arrays

4 ビュー (過去 30 日間)
ErikJ GiesenLoo
ErikJ GiesenLoo 2024 年 2 月 22 日
コメント済み: ErikJ GiesenLoo 2024 年 2 月 22 日
Hi all,
I have found i can create a struct of empty cell arrays as follows (in R2018b):
struct("a", {cell(0)}, "b", {cell(0)})
I was wondering if this is the most "MATLABic" way of doing that and why the following does not work:
struct("a", {}, "b", {})
or
struct("a", cell(0), "b", cell(0))
Thanks!!

採用された回答

Stephen23
Stephen23 2024 年 2 月 22 日
編集済み: Stephen23 2024 年 2 月 22 日
"I was wondering if this is the most "MATLABic" way of doing that"
Yes.
"...and why the following does not work"
Because by definition of STRUCT() the outermost cell array determines the size of the structure array (and are therefore removed). Any remaining nested cells remain as the field data.
Otherwise there would be no simple way to define non-scalar structures using STRUCT().
Note that STRUCT() actually does "work" exactly as documented: "If value is a nonscalar cell array, then s is a structure array with the same dimensions as value. Each element of s contains the corresponding element of value. For example, s = struct('x',{'a','b'}) returns s(1).x = 'a' and s(2).x = 'b'."
  1 件のコメント
ErikJ GiesenLoo
ErikJ GiesenLoo 2024 年 2 月 22 日
ok. makes sense - it just looked very strange

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by