Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct fields from cell array

Tags:

matlab

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.

like image 966
Amelio Vazquez-Reina Avatar asked Oct 24 '11 14:10

Amelio Vazquez-Reina


People also ask

Can a field in a struct be an array?

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.

How do you turn a cell array into a structure in Matlab?

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.

How is a cell array different from a struct?

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.

How do you access a field in a struct in Matlab?

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.


1 Answers

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: []
like image 157
Clement J. Avatar answered Oct 06 '22 08:10

Clement J.