First, note that with your example, at the end p is a structure with just one field 'p10'. To create the structure you want:
p = struct('p1', {p1}, 'p2', {p2}, 'p3', {p3}, ...
Also note that you don't need to put the field contents into a cell array, unless this content is itself a cell array. so:
p = struct('p1', p1, 'p2', p2, ...
may work just as well.
Secondly, your for loop is not going to work, ['p1' 'p2' 'p3' ...] is exactly the same as 'p1p2p3....', and i will take in turn the values 'p', then '1', then 'p' again, then '2', etc. You need to use a cell array
Finally, to use dynamic field names, you enclose the variable holding the field name in round brackets ().
So:
p = struct('p1',{p1}, 'p2',{p2}, 'p3',{p3}, 'p4',{p4}), 'p5',{p5}, 'p6',{p6}, 'p7',{p7},
'p8',{p8}, 'p9',{p9}, 'p10',{p10});
for fn = {'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9', 'p10'}
o = p.(fn{1});
end
Best Answer