Skip to main content

getFieldByName.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function returns the first instance of a field matching the given
% name and can be used to find a field in a struct that has been parsed
% from and XML (xml2struct)
%
% (struct, field) => (struct)
% Find first instance of field in nested struct


function [output, path] = getFieldByName(deepStruct, field)
output = false;
path = [field];
try
output = deepStruct.(field);
return
catch
end
if(isstruct(deepStruct))
fields = fieldnames(deepStruct);
for i=1:length(fields)
[output, path] = getFieldByName(deepStruct.(fields{i}),field);
if(isstruct(output) || iscell(output))
path = [string(fields{i}) path];
return
end
end
end
end