Using Set Field for multiple depths
32 次查看(过去 30 天)
显示 更早的评论
Hi,
I'd like to set a particular element of a structure array with a dynamic index to a field. So, for example, if fieldNames={'a' 'b' 'c'} and fieldValue=3, I want to set myStruct.a.b.c=3. I can hack together something that uses the eval function, say for example
str=['myStruct'];
for ii=1:length(fieldnames)
str=[str '.('''];
str=[str fieldnames{ii}];
str=[str ''')'];
end
eval([str '=' num2str(fieldValue)])
But is there a way to do this without using eval? I tried looking at setfield, but I can't get it to work. Running
x.a.b.c=3;
fieldnames={'a' 'b' 'c'};
getfield(x,fieldnames{:})
successfully returns 3, but running
setfield(x,fieldnames{:},5)
doesn't seem to do anything
Thanks
Brendan
0 个评论
采纳的回答
Walter Roberson
2012-5-9
You will not be able to do this using setfield() or getfield(), not in any useful way.
You should refer to subsref() and subsassgn(). They are a bit clumsy to use, but they can handle the task.
更多回答(1 个)
Daniel Shub
2012-5-9
If it is always 3 deep then you can just do
myStruct.(fieldnames{1}).(fieldnames{2}).(fieldnames{3}) = fieldValue;
A little bit more general is
switch length(fieldnames)
case 1
myStruct.(fieldnames{1}) = fieldValue;
case 2
myStruct.(fieldnames{1}).(fieldnames{2}) = fieldValue;
case 3
myStruct.(fieldnames{1}).(fieldnames{2}).(fieldnames{3}) = fieldValue;
end
You can obviously extend this to any N you want.
A truly robust solution would be to do this recursively.
function S = rsetfield(S, field, V)
if length(field) > 1
S.(field{1}) = rsetfield(S.(field{1}), field(2:end), V);
else
S.(field{1}) = V;
end
end
You would want to do some input checking to make sure everything is happy. I have no idea how inefficient this is.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!