I do have a simple question, but it doesn’t go for me.
I want to preallocate a structure that contains many fields. I want to obtain it like this:
structure S 1×30 and each of 30 fields should be a structure 1×50 (some of 50 entries integers, some cells with strings, some subarrays). Is this possible to preallocate it without giving the exact names for each of 50 fields?
cheers!
AFAIK struct fields must be named, however, those names don’t need to be hard coded.
For example if I have a struct
foothat has a field namedbarI can access that field like so:The
foo.(name)notation indicates that one can make field names from variables (dynamic field names) for which the doc can be found here.Additionally you can use this to create fields.
The struct
foonow has 10 fields namedbar1,bar2, …bar10.If you absolutely don’t want names and simply want indicies then what you probably want is a cell array. Cell arrays are like regular matlab vectors except that they can contain anything.
For example
cis a cell array that contains a string, scalar, vector, matrix and then function handle, and another cell array.You can access the contents of an individual cell using the curly braces
{}. Soc{1}would return'1234'whereasc{2}would return a number.You could use either of these methods to pre-allocate a data structure that fits what you’ve described.