How to create struct of cell arrays

3 views (last 30 days)
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!!

Accepted Answer

Stephen23
Stephen23 on 22 Feb 2024
Edited: Stephen23 on 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'."

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!