MATLAB Answers

Assigning a value to certain indexed elements of a struc variable

1 view (last 30 days)
z8080
z8080 on 11 Mar 2018
Commented: z8080 on 12 Mar 2018
I am trying to assign a certain value to several elements of an indexed structure:
indices = 1:3
people(indices).gender ='m'
..but get the error
"Expected one output from a curly brace or dot indexing expression, but there were 3 results."
Is this a limitation of how Matlab can work with structures, or am I making a mistake in how I am using these variables?
Is this the (still not implemented) enhancement described in this other thread reporting the same error?
Thanks for any help! I am using 2016b.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Mar 2018
[people(indices).gender] = 'm';

  6 Comments

Show 3 older comments
Jos (10584)
Jos (10584) on 12 Mar 2018
Just to show you another way, which can be used when you initialise the struct array:
people(1:3) = struct('gender','m') ;
Walter Roberson
Walter Roberson on 12 Mar 2018
Note that Jos's solution only works for the initial creation of the output, not for setting a field within an existing structure

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!