How to create struct of cell arrays
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ErikJ GiesenLoo
el 22 de Feb. de 2024
Comentada: ErikJ GiesenLoo
el 22 de Feb. de 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!!
0 comentarios
Respuesta aceptada
Stephen23
el 22 de Feb. de 2024
Editada: Stephen23
el 22 de Feb. de 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'."
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!