Using Set Field for multiple depths
조회 수: 26 (최근 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!