How to create struct of cell arrays

3 vues (au cours des 30 derniers jours)
ErikJ GiesenLoo
ErikJ GiesenLoo le 22 Fév 2024
Commenté : ErikJ GiesenLoo le 22 Fév 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!!

Réponse acceptée

Stephen23
Stephen23 le 22 Fév 2024
Modifié(e) : Stephen23 le 22 Fév 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 commentaire
ErikJ GiesenLoo
ErikJ GiesenLoo le 22 Fév 2024
ok. makes sense - it just looked very strange

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by