Using Set Field for multiple depths

28 ビュー (過去 30 日間)
Brendan
Brendan 2012 年 5 月 8 日
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

採用された回答

Walter Roberson
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 件のコメント
Brendan
Brendan 2012 年 5 月 11 日
Thanks for the subsref comment. I don't really understand your comment about getfield(), it seems like I can use getfield in the way I describe, just not setfield.

サインインしてコメントする。

その他の回答 (1 件)

Daniel Shub
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.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by