MATLAB: Nested structure

nested structure

I am interested in reading data from a field of a structure which is within many layers of structures. data = info.ROIcontour1.Slice1.contourdata; This fetches the contourdata from slice#1 of the first ROI. I would like to read all the slices of that ROI and store inside 'data'. Obviously, doing this manually for 100s of slices is not fun. I am not aware of a solution using a simple For loop. Suggest me ways.
Thanks, Ganesh

Best Answer

fn = fieldnames(info.ROIcontour1);
fn = fn(strncmp(fn,'Slice',5));
nf = length(fn);
contourslices = cell(nf,1);
for K = 1 : nf
countourslices{K} = info.ROIcontour1.(fn{K}).contourdata;
end