MATLAB: How to access randomly selected index in nested structures

MATLABnested structures

I want to access intexes that are into a structure that are nested within another structure. I have this code working through every ith element, and I want to add to the code randomly selected values j. In my case j is from 1 to 365 day of the year.
for i=1:n
fileName=structName(i).nestedStructName(j).name
end
Any ideas?
Darina

Best Answer

Try something like this:
for i=1:n
X = fieldnames(structName);
n = length(X);
fileName=structName(i).(X{randi(n)}).name;
end
The issue is that your nestedStucture has multiple fields and you wanna pick one randomly.