Convert string of nested field names to variable without eval?
Sorry if this is answered elsewhere, It wasn’t obvious to me from searching the archives that this exact question has been posed before.
I am parsing XML files using the function in File Exchange xml2struct (https://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct) which is a great tool, BTW.
As you might expected, the output is a nested structure, and depending on the input file, the field names and complexity of the structures are highly variable.
I am searching through theses structures to find a particular field of interest:
S.Data.Configuration.FieldofInterest
but in another file, that field might be on a completely different branch or level:
S2.Data.Nested.Field.FieldofInterest
I can generate list of strings with all the field names which I can parse to find the field of interest. But then I have some strings like this:
fnlist = ‘S.Data.Configuration.FieldofInterest’;
fnlist2 = ‘S2.Data.Nested.Field.FieldofInterest’;
If I want to extract the data from the fields of interest, the only way I know how to do this is to use eval:
output = eval(fnlist);
I’m not a fan of the eval functions because they make it very hard to debug code and diagnose problems if the string gets malformed.
I’d like to use dynamic field names like S.(name1).(name2).(name3), etc., but unless you know a priori the data structure and how many levels in your target field is (which I will not), this isn’t possible.
Is there another alternative besides eval? Thanks in advance.Sorry if this is answered elsewhere, It wasn’t obvious to me from searching the archives that this exact question has been posed before.
I am parsing XML files using the function in File Exchange xml2struct (https://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct) which is a great tool, BTW.
As you might expected, the output is a nested structure, and depending on the input file, the field names and complexity of the structures are highly variable.
I am searching through theses structures to find a particular field of interest:
S.Data.Configuration.FieldofInterest
but in another file, that field might be on a completely different branch or level:
S2.Data.Nested.Field.FieldofInterest
I can generate list of strings with all the field names which I can parse to find the field of interest. But then I have some strings like this:
fnlist = ‘S.Data.Configuration.FieldofInterest’;
fnlist2 = ‘S2.Data.Nested.Field.FieldofInterest’;
If I want to extract the data from the fields of interest, the only way I know how to do this is to use eval:
output = eval(fnlist);
I’m not a fan of the eval functions because they make it very hard to debug code and diagnose problems if the string gets malformed.
I’d like to use dynamic field names like S.(name1).(name2).(name3), etc., but unless you know a priori the data structure and how many levels in your target field is (which I will not), this isn’t possible.
Is there another alternative besides eval? Thanks in advance. Sorry if this is answered elsewhere, It wasn’t obvious to me from searching the archives that this exact question has been posed before.
I am parsing XML files using the function in File Exchange xml2struct (https://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct) which is a great tool, BTW.
As you might expected, the output is a nested structure, and depending on the input file, the field names and complexity of the structures are highly variable.
I am searching through theses structures to find a particular field of interest:
S.Data.Configuration.FieldofInterest
but in another file, that field might be on a completely different branch or level:
S2.Data.Nested.Field.FieldofInterest
I can generate list of strings with all the field names which I can parse to find the field of interest. But then I have some strings like this:
fnlist = ‘S.Data.Configuration.FieldofInterest’;
fnlist2 = ‘S2.Data.Nested.Field.FieldofInterest’;
If I want to extract the data from the fields of interest, the only way I know how to do this is to use eval:
output = eval(fnlist);
I’m not a fan of the eval functions because they make it very hard to debug code and diagnose problems if the string gets malformed.
I’d like to use dynamic field names like S.(name1).(name2).(name3), etc., but unless you know a priori the data structure and how many levels in your target field is (which I will not), this isn’t possible.
Is there another alternative besides eval? Thanks in advance. struct, eval, field names MATLAB Answers — New Questions