Effacer les filtres
Effacer les filtres

How to access randomly selected index in nested structures

1 vue (au cours des 30 derniers jours)
Darina
Darina le 28 Jan 2014
Commenté : Darina le 9 Fév 2014
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

Réponse acceptée

Amit
Amit le 28 Jan 2014
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.

Plus de réponses (2)

Mischa Kim
Mischa Kim le 28 Jan 2014
How about
fileName=structName(i).nestedStructName(randi(365,1)).name
Is this what you are looking for?
  7 commentaires
Darina
Darina le 28 Jan 2014
[rS, cS] = size(allFileMin)
[rnS, cnS] = size(allFileMin(rS).demand_pattern)
the answer:
rS =
1
cS =
28
Reference to non-existent field 'demand_pattern'.
Error in Activity (line 136)
[rnS, cnS] = size(allFileMin(rS).demand_pattern)
Darina
Darina le 28 Jan 2014
This happends because I only have nestedStructName1 to nastedStructName365 but not a single naestedStructName without index.

Connectez-vous pour commenter.


Shivaputra Narke
Shivaputra Narke le 28 Jan 2014
May this works,
for i=1:n j=1+round(364*rand(1)); fileName=structName(i).nestedStructName(j).name end
  1 commentaire
Darina
Darina le 28 Jan 2014
Hm, no it still gives the same error.. -.-

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by