0

I'm having the following problem which I can't believe there to be a simple solution for. At any rate a google search wasn't succesful.

Suppose I make the following struct

teststruct = struct('field1',{'a','b','c'});

Now I want to add another field called 'field2' filled with 'd', 'e' and 'f'. Basically I want to achieve the result I would have achieved by directly doing.

teststruct2 = struct('field1',{'a','b','c'}, 'field2', {'d','e','f'});

I tried several solutions and the closest I got was

[teststruct(:).field2] = {'d';'e';'f'};

This however puts the entire cell on the right hand side in it's entirety in the objects 'field2' field.

Does anyone know a solution?

1 Answer 1

2

Just lift out the instantiation

teststruct.field1 = {'a','b','c'}
teststruct.field2 = {'d','e','f'}
Sign up to request clarification or add additional context in comments.

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.