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.

 采纳的回答

"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 个评论

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:
"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>
@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.

请先登录,再进行评论。

更多回答(1 个)

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

1 个评论

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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by