How do I make a function accept structure and fields as separate inputs?

1 次查看(过去 30 天)
I have a structure that is organised something like this:
Subject(index).DataType.DataSubType.means
And I want to run a custom-written function on it.
Part of the function is to loop through all the elements in the structure:
for index = 1:20
%do something to Subject(index).DataType.DataSubType.means
end
I would also like this function to work on different, similarly organised data, e.g. Differentstructure(index).DataType.DataSubType.SDs. Thus I want to be able to give the overall name of the structure ('Subject') and the particular field ('DataType.DataSubType.means') as separate inputs to the function.
Is there a way to do this? I haven't been able to find one. Eval doesn't work, and I'm aware it isn't good practice anyway. If not, how should I organise my function to do this?

采纳的回答

Guillaume
Guillaume 2015-11-27
You can use dynamic field names for this, but to be honest eval is probably the best solution in this case.
Here it is with dynamic field names. You can use different methods to specify the list of fields to extract (one big string, a cell array, etc.), they're all essentially the same:
function out = iteratestruct(sarr, fieldhierarchy, fun)
%iteratestruct Apply function to subfields of a structure array
%sarr: structure array with fields that match fieldhierarcy
%fieldhierarchy: string indicating which fields to extract. Fields are separated by '.'
%fun: a function handle which takes value from the specified field and return one output
fieldlist = strsplit(fieldhierarcy, '.');
out = cell(size(sarr));
for sidx = 1:numel(sarr) %iterate over structure array
selem = sarr(sidx);
for f = fieldlist %navigate the field hierarchy
selem = surelem.(f{1});
end
out(sidx) = fun(selem); %selem is the required field value
end
end
Whereas with eval:
function out = iteratestruct(sarr, fieldhierarchy, fun)
%iteratestruct Apply function to subfields of a structure array
%sarr: structure array with fields that match fieldhierarcy
%fieldhierarchy: string indicating which fields to extract. Fields are separated by '.'
%fun: a function handle which takes value from the specified field and return one output
out = cell(size(sarr));
for sidx = 1:numel(sarr)
selem = sarr(sidx);
out = eval(sprintf('fun(selem.%s)', fieldhierarchy));
end
end
Code is untested, may have typos or bugs, and of course lacks any kind of input check / argument validation, error handling, etc.
  1 个评论
Isobel
Isobel 2015-11-27
Thanks very much for your answer. In the end, I had success with getfield.
getfield(main_structure, {index}, sub_fields{:},{1, 1}
I also needed a cell2mat.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by