Say I have a cell array:
my_cell_array = {'Jimmy', 'Timothy', 'Charles', ...}
Is there a compact way of defining a single struct
that has the items of my_cell_array
as fieldnames? The members of the new struct can hold empty cells or empty arrays.
The names and number of fields within the STRUCT are fixed. Each field can be a different type. A field within a STRUCT can also be another STRUCT , or an ARRAY or a MAP , allowing you to create nested data structures with a maximum nesting depth of 100.
To create a structure array with fields derived from M columns of a cell array, specify M field names in the fields argument and the number 2 in the dim argument. The structArray output is a structure array with N fields, where N is equal to the number of fields in the fields input argument.
A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data.
Access Field of Scalar StructureWhen you use the getfield function, you can access a field of the structure returned by a function without using a temporary variable to hold that structure. You also can access a field using dot notation.
cell2struct
is probably what you need.
my_cell_array = {'Jimmy', 'Timothy', 'Charles'}
s = cell2struct(cell(size(my_cell_array)), my_cell_array, 2)
s =
Jimmy: []
Timothy: []
Charles: []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With