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.

 Respuesta aceptada

Stephen23
Stephen23 el 17 de Sept. de 2026 a las 12:14
Editada: Stephen23 el 17 de Sept. de 2026 a las 12:17
"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:

6 comentarios

Stephen23
Stephen23 el 17 de Sept. de 2026 a las 12:23
Editada: Stephen23 el 17 de Sept. de 2026 a las 12:31
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.
Stephen23
Stephen23 el 17 de Sept. de 2026 a las 12:56
Editada: Stephen23 el 17 de Sept. de 2026 a las 13:06
"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>
Wassili Dimitriew
Wassili Dimitriew el 21 de Sept. de 2026 a las 7:26
Editada: Wassili Dimitriew el 21 de Sept. de 2026 a las 7:27
@dpb, well, the structure that I have (a cell array of scalar structures) is something like a standard in my community, so I will just stick to the "for"-loop.

Iniciar sesión para comentar.

Más respuestas (1)

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

1 comentario

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.

Iniciar sesión para comentar.

Productos

Versión

R2025b

Preguntada:

el 17 de Sept. de 2026 a las 12:03

Editada:

el 21 de Sept. de 2026 a las 7:27

Community Treasure Hunt

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

Start Hunting!

Translated by