How to access randomly selected index in nested structures
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Amit
2014-1-28
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.
更多回答(2 个)
Mischa Kim
2014-1-28
How about
fileName=structName(i).nestedStructName(randi(365,1)).name
Is this what you are looking for?
Shivaputra Narke
2014-1-28
May this works,
for i=1:n j=1+round(364*rand(1)); fileName=structName(i).nestedStructName(j).name end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!