Search code examples
arraysmatlabstructurecell

How to add a Field in structure array?


I have a cell array that has

'Player'    {1x3 cell}    'Position'    {1x3 cell}

I would like to create a structure array using this. The field names are supposed to be 'Player' and 'Position'. I can't use struct() to create it. This is what I tried

for ii = 1:length(sparta)
        kra = cell(ii); %assessing what is inside {1X3 cell}
        out(ii).feildd = kra;
end

When I try this I get a field name of fieldd instead of Player and Position.

Note: I can have anything inside my cell. 'Player and 'Position' are just an example


Solution

  • If I understand your question correctly, you have a cell array that has 2*N elements. The odd-index elements should be turned into field names of a structure and the following even-index elements should be the values of those fields. So assuming that your cell array is called "sparta", try this:

    spartaStruct = struct(sparta{:});