How to access elements with same field names in an array of structures without using a loop?

Hello,
Assume we have a cell array of structures:
a.name = 'first';
b.name = 'second';
c.name = 'third';
s = {a; b; c};
Now I want to change the values of fields "name" in some specific structures, for example, in 1 and 3. I know how to do it using a "for"-loop:
ind = [1,3];
replacement = {'whatever', 'word'};
for i = 1:length(ind)
s{ind(i)}.name = replacement{i};
end
Is there a way to do it without a loop? I have found a similar question, but there the author was interested in extracting the data, not in replacing them. Frankly, I am bad in both writing and reading handles' syntaxis, so maybe this answer can be applied to my problem as well, and I just do not see how. Plus, as was mentioned in the comments, cellfun is not faster than a loop - and, at least for me, not easier to read either.

 Accepted Answer

"How to access elements with same field names in an array of structures without using a loop?"
There is no way using your data, because you actually showed a cell array of scalar structures (which is not very good data design if all of the scalar structures have the same fields). Much better would be to use a non-scalar structure array:
s(1).name = 'first';
s(2).name = 'second';
s(3).name = 'third';
s.name % checking
ans = 'first'
ans = 'second'
ans = 'third'
in which case your request is certainly possible:
idx = [1,3];
rpl = {'whatever', 'word'};
[s(idx).name] = deal(rpl{:});
s.name % checking
ans = 'whatever'
ans = 'second'
ans = 'word'
For more information:

5 Comments

However a cell array of scalar structures (like you showed) will not work:
a.name = 'first';
b.name = 'second';
c.name = 'third';
ThisIsACellArrayNotAStructureArray = {a; b; c};
idx = [1,3];
rpl = {'whatever', 'word'};
[ThisIsACellArrayNotAStructureArray{idx}.name] = deal(rpl{:});
Intermediate brace '{}' indexing produced a comma-separated list with 2 values, but it must produce a single value when followed by subsequent indexing operations.
for the reasons explained here:
(the explanation is generally valid for any nested container arrays, not just structure arrays).
Thank you for the answer. Unfortunately, the structures in my cell array (the one I am actually working with) are the scalar ones. On top of that, I cannot be sure that all the structures have the same number/positioning of fields; as far as I know, that means I cannot convert my structures into non-scalar ones.
"I cannot be sure that all the structures have the same number/positioning of fields"
Only the fieldnames are really relevant.
If you prefer to stick with scalar structures in a cell array then you will have to use an implict or explicit loop.
See also:
Thank you for the links, I will give them a look.
"I cannot be sure that all the structures have the same number/positioning of fields"
Perhaps instead of the cell array of disparate but apparently somehow related scalar struct, use a nested struct that incorporates the alternate data fields by what identifies the particular quantities? <Address Nested Structures>

Sign in to comment.

More Answers (1)

You could possibly work something up using cellfun with a subsasgn call, but it would be ugly and needlessly complex.

1 Comment

And also this suggestion comes with the caveat that it will be slower than a loop, have more overhead, and will be harder to maintain/update or debug.

Sign in to comment.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Products

Release

R2025b

Asked:

on 17 Sep 2026 at 12:03

Commented:

Rik
on 18 Sep 2026 at 19:39

Community Treasure Hunt

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

Start Hunting!