8

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.

2 Answers 2

8

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: []
Sign up to request clarification or add additional context in comments.

2 Comments

I looked into it, but cell2struct seems to be designed to build struct arrays from cell arrays. What I want is a single struct with field names from the cell array. That said, you are probably right, there is probably a way of doing this with cell2struct. I'll look deeper into it.
Got it. Yes, thanks! I'll accept this as soon as it lets me do it.
0

Try using this statement:

cell2struct(cell(size(my_cell_array)),my_cell_array,2)

It returns:

ans = 

      Jimmy: []
    Timothy: []
    Charles: []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.