Want to combine 4 structures into 1?
2 views (last 30 days)
Show older comments
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 Comment
Jan
on 19 Jun 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.
Accepted Answer
Guillaume
on 19 Jun 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 Comments
Jan
on 19 Jun 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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!