Here is a solution. You'll have to write the functions differently for gathering cells and arrays.
fun = @(s,field) {s.(field)};
output = fun([Patient.Location],'state');
fun2 = @(s,field) [s.(field)];
output = fun2([Patient.Vitals],'weight');
And this even works in R2019b:
f1 = @(s,field) [s.(field)];
f2 = @(s,field,subfield) [f1(s,field).(subfield)];
output = f2(Patient,'Vitals','weight');
But this essentially does the same thing as creating a temporary struct, [Patient.Vitals], and then indexing into that. It can just be neatly arranged into an anonymous function.
There is probably also a way to do this with subsref, but it would probably get complex.
1 Comment
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/491775-accessing-field-data-in-nonscalar-structure-array#comment_768810
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/491775-accessing-field-data-in-nonscalar-structure-array#comment_768810
Sign in to comment.