Want to combine 4 structures into 1?

2 vues (au cours des 30 derniers jours)
MIRANDA WHAH
MIRANDA WHAH le 19 Juin 2017
Commenté : Jan le 19 Juin 2017
Hello, I currently have 4 structures: (Human1 [1x13], Auto1 [1x9], Human2 [1x1], Auto2 [1x1]). I should note, each of these structs has 4 fields in the (A;B;C;D). I want to combine these into a single struct called AllSituations. I want to be able to click on all situations and see first categories 1 and 2, and when you click into 1 you see Human1 and Auto1 and same with 2. Then when you click on Human1 or any of them you can see their struct listed.
I don't know how to combine structures within structures, so I was hoping for some help!
  1 commentaire
Jan
Jan le 19 Juin 2017
The question is not clear: "click into 1 you see Human1 and Auto1". Currently the "1" appears in the name of the struct only. After "combining" the structs, the new struct must have a new name and then the "1" and "2" information vanishes. It was a very bad idea to include this information in the name of the variable.
It is not clear, where you want to "click" on what. Clicking implies a GUI.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 19 Juin 2017
You shouldn't have numbered your variables in the first place. That only leads to problems. Put them together in an array.
If I understood correctly, this should give you more or less what you want:
Allsituations.Human = Human1;
Allsituations.Auto = Auto1;
Allsituations(2).Human = Human2;
Allsituations(2).Auto = Auto2;
Or as a more efficient but slightly more obscure one liner:
Allsituations = struct('Human', {Human1, Human2}, 'Auto', {Auto1, Auto2});
  3 commentaires
MIRANDA WHAH
MIRANDA WHAH le 19 Juin 2017
Sorry for using numbers in the example! The real names of my variables are not those, I just thought it would be easier for explaining purposes.
My actual variables are:
CriticalCutAuto
CriticalCutHuman
CriticalFolAuto
CricticalFolHuman
and I want to put them in a struct called All Situations, to which you can click and 2 categories pop up: Follow and Cut. If you were to click on Follow, you would then see CriticalFolAuto and CriticalFolHuman, and when you click on one of those you see their original strutures.
I think the information you both gave me should work just fine. Sorry for any confusion there!
Jan
Jan le 19 Juin 2017
Even if the key is not a number, storing relevant data in the name of teh variable is a bad idea. Use variables instead:
Event(1).Class = 'Cut'
Event(1).Object = 'Auto'
Event(2).Class = 'Cut'
Event(2).Object = 'Human'
Event(3).Class = 'Fol'
Event(3).Object = 'Auto'
Event(4).Class = 'Cut'
Event(4).Object = 'Human'
Do you see it? Then the combination of many "events" is trivial.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by