How to create struct of cell arrays

3 Ansichten (letzte 30 Tage)
ErikJ GiesenLoo
ErikJ GiesenLoo am 22 Feb. 2024
Kommentiert: ErikJ GiesenLoo am 22 Feb. 2024
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!!

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Feb. 2024
Bearbeitet: Stephen23 am 22 Feb. 2024
"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 Kommentar
ErikJ GiesenLoo
ErikJ GiesenLoo am 22 Feb. 2024
ok. makes sense - it just looked very strange

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by