Assigning to a field in a multi-dimensional struct array with indices in a cell array

2 views (last 30 days)
I am curious about an inconsistency that I observe in the following assignment to a multi-dimensional struct array when the indices are generated from a comma separated list expansion of a cell array.
V(1:2,1:2,1:2) = struct('a',2);
U = V;
[U(:,2,:).a] = deal(3);
disp({U.a}); % (correct) output: [2] [2] [3] [3] [2] [2] [3] [3]
I = {':',2,':'};
U = V;
[U(I{:}).a] = deal(3);
disp({U.a}); % (incorrect) output: [3] [3] [3] [3] [3] [3] [3] [3]
J = {1:2,2,1:2};
U = V;
[U(J{:}).a] = deal(3);
disp({U.a}); % (incorrect) output: [3] [3] [2] [2] [2] [2] [2] [2]
Any explanation as to what is going on? In my code, I need such an assignment where the index range to the struct array need to be dynamically generated. The only way that I have got things to work right now is to use a temporary struct to split the one step assignment to 3 steps as below.
U = V;
T = U(J{:}); % or T = U(I{:})
[T.a] = deal(3);
U(J{:}) = T;
disp({U.a}); % (correct) output: [2] [2] [3] [3] [2] [2] [3] [3]

Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!