MATLAB Answers

How to update struct array fields with mutiple values

616 views (last 30 days)
I am trying to update a field value in a struct array. For example if I have 1*10 struct of A with a field in it called B, I want to replace the following loop with another method:
for iLoop=1:10
A(iLoop).B = iLoop;
I tried:
[A.B] = deal(1:10);
And also:
A = setfield(A,num2cell(1:10),'B',num2cell(1:10),(1:10));
But none of them worked (the first method assigns the whole (1:10) vector to each 'B' field in the struct array. The second one crashes). Does anyone know how to make it work?


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Mar 2012
Edited: Walter Roberson on 19 Sep 2019
t = num2cell(1:10);
[A.B] = t{:};
See comments for the case where the struct does not already exist.


Show 3 older comments
breathi on 19 Sep 2019
please edit your answer and insert the solution you posted in the comments.
It was driving me nuts to find out, that only an existing struct array can be filled with your current solution, just to find out a few angry debug steps later that you posted a comment and that this solution could just create a new struct array by explicitly using length(t) as the left side input.
Walter Roberson
Walter Roberson on 19 Sep 2019
For the case where the struct does not already exist, there is a different method that can be easier:
A = struct('B', t);
where t is the cell array from above. This also permits you to store to multiple fields, and to leave some fields empty, and to put in non-scalar values.
A = struct('B', num2cell(1:10), 'C', [], 'D', num2cell(rand(2,10),1));

Sign in to comment.

More Answers (3)

Andrew Newell
Andrew Newell on 5 Mar 2012
What timing! It happens that the File Exchange Pick of the Week is the function disperse. If you download disperse and put it on your path, you can use the following command:
[A.B] = disperse(1:10);


Mohammad Tabesh
Mohammad Tabesh on 5 Mar 2012
Thank you for your help but Walter's suggestion works perfectly and does not need any functions.
Brad Stiritz
Brad Stiritz on 15 Sep 2018
Thank you Andrew! disperse() is just what I needed.
Stephen Cobeldick
Stephen Cobeldick on 16 Sep 2018
"disperse() is just what I needed."
Actually Walter Roberson's answer does exactly the same thing, without requiring third-party functions.

Sign in to comment.

Tom on 12 Feb 2018
Edited: Tom on 12 Feb 2018
This works perfectly, until you have a field in between.
[A.B] = t{:}; %Great!
[A.B(1).C] = t{:}; % doesn't work
Anyone have a suggestion on how to make it work this way?

  1 Comment

Mohammad Tabesh
Mohammad Tabesh on 13 Feb 2018
If your question structure is that A has 10 rows, each have a field B which in turn has a field C that you want to update, I suggest creating an intermediate structure array similar to B, assign the values there and then assign that to field B:
D = A.B;
[D.C] = t{:};
[A.B] = deal(D);

Sign in to comment.

Mitja M
Mitja M on 25 Jul 2018
I would highly appreciate the solution to the following problem, which I believe is highly related to the previous, but I somehow don't find the appropriate solution.
I have the following variables:
AA, 1×3 struct array with fields: bb
bb, nx6 double array
cc, nx3 double array
n for all three bb and cc arrays is equal to 100 right now
Now I would like to change the fifth column of all three bb arrays to corresponding column in cc array. It can be correctly done using the following for loop:
for i=1:3
Is it possible to achieve this without the for loop.
Thank you

  1 Comment

Mohammad Tabesh
Mohammad Tabesh on 26 Jul 2018
Hi Mitja, I suggest a similar approach as I suggested to Tom.
BBB=num2cell(BB,[1 2]);

Sign in to comment.

Sign in to answer this question.

Translated by