MATLAB: Match structures by table

matchingstructurestable

Dear All, suppose you have two structures, A and B, where
A.a
A.b
A.c
and
B.1
B.2
B.3
Furthermore you have a table, which matches A to B, for example
1 b
2 a
3 c
bsed on this information, I would like to compute stuff using A and B. Both structures have the same number of fiels (which can all be uniquley matched between A and B). I need something like:
for all *table* rows i:
% computation using A.i, B.i;
end
Is there a good way to solve this in Matlab?
Thanks in advance!

Best Answer

You can do this easily using dynamic fieldnames to access the structures. I do not have a MATLAB version with tables, but this example using a cell array should get you started:
A.a = 1;
A.b = 2;
A.c = 3;
B.x = 4;
B.y = 5;
B.z = 6;
C = {'b','x';'a','y';'c','z'};
out = NaN(size(C,1),1);
for k = 1:size(C,1)
af = C{k,1};
bf = C{k,2};
out(k) = A.(af) + B.(bf);
end
produces the correct result:
>> out
out =
6
6
9