Combining structs into a larger struct?
Show older comments
Okay, I have 4 structs.
CriticalCutAuto %(1x1 with 4 fields called A;B;C;D)
CriticalCutHuman %(1x1 with 4 fields called A;B;C;D)
CriticalFolAuto %(1x13 with 4 fields called A;B;C;D)
CricticalFolHuman %(1x9 with 4 fields called A;B;C;D)
There are 2 categories (Cut Situations and Follow Situations) with 2 subparts of data (Human and Auto). In my current structs, say in CritFolAuto, you can scroll over the box in field A row 1, and click to see the data, which is a 1x100 double.
What I want to be able to do is click on the name AllSituations in the workspace, and a 1x2 struct appears that says 'Follow' and 'Cut'. I then want to be able to CLICK on either of those two files to see another 1x2 struct with 'Human' and 'Auto'. When I CLICK on one of those two, I would like to see the original structs for CriticalCutHuman and the rest in their respective spots, so when I go into that data I can see the four fields A;B;C;D and the data that lies inside of them.
I thought I could call each struct with writing the name and '= []' to call it but I think that is wrong:
function [ AllSituations] = CritSituations( AutomatedData, HumanData )
CritSituations = [];
CriticalCutAuto= [] ;
CriticalCutHuman= [] ;
CriticalFolAuto= [] ;
CriticalFolHuman= [] ;
AllSituations(1).Follow = CriticalFolHuman;
AllSituations(1).Follow = CriticalFolAuto;
AllSituations(2).Cut = CriticalCutHuman;
AllSituations(2).Cut = CriticalCutAuto;
AllSituations = ans
%Critical_Situations = struct('Following Situations', {CriticalFolAuto, CriticalFolHuman}, 'Cut Situations', {CriticalCutAuto, CriticalCutHuman});
end
I also tried the silenced line but that did not work at all.
6 Comments
MIRANDA WHAH
on 19 Jun 2017
Jan
on 19 Jun 2017
The question is still not clear. What does "click into AllSituations" mean? Does your problem concern the representation of the data as an arrays or a GUI? It does not matter for the solution, that all variables start with "Critical", so the code and the question could be much leaner.
You do not explain, how you call your function and what is provided as "AutomatedData, HumanData". But these variables do not appear in the code. Using "ans" is a vary strange idea and the commented code line uses fieldnames, which contain a space and this should give you a clear error message.
AllSituations(1).Follow = CriticalFolHuman;
AllSituations(1).Follow = CriticalFolAuto;
This overwrites the variables AllSituations(1).Follow directly after the creation - but why? I cannot guess the prupose.
"I have CriticalCutAuto CriticalCutHuman ..." does not explain anything. Please post some Matlab code, which creates the variables with dummy data. Are they cells, numerical arrays or nested structs? What do you expect as output? What is the point of "clicking" on what?
Please edit the question and insert useful details.
MIRANDA WHAH
on 19 Jun 2017
Jan
on 20 Jun 2017
Of course we will be patient and questions about Matlab are welcome in this forum.
The question contains this sentence:
you can scroll over the box in field A row 1, and click to see
the data
Are you talking about structs or a GUI? Where do you scroll and click? You mean the work space browser? Ah, an important detail.
Nevertheless, I still do not understand, what you are looking for and the purpose of the posted code.
Structs are a kind of cell arrays, which are not accessed by indices, but by fieldnames. I think understanding, how structs work, is essential for you.
"Structs are kind of cell arrays"
I think it would be more useful to explain that both cell arrays and structures are different types of Containers (as structures are not really a kind of cell array).
Jan
on 20 Jun 2017
@Stephen: You are right, this was a lazy formulation. In the MEX level structs and cells are very similar and the struct2cell conversion is really fast in consequence. But to see both as containers is more useful. I only wanted to encourage Miranda to get familiar with structs.
Answers (1)
Hadeel Nourahmed
on 7 May 2024
0 votes
% Function to merge structures
function mergedStruct = mergeStructures(struct1, struct2)
% Merge the fields of the two structures
fields1 = fieldnames(struct1);
fields2 = fieldnames(struct2);
for j = 1:length(fields2)
if isfield(struct1, fields2{j})
% Field name already exists in struct1, rename it in struct2
field2new = [fields2{j} '_folder2']; % Or any unique identifier
struct2.(field2new) = struct2.(fields2{j});
struct2 = rmfield(struct2, fields2{j});
end
end
% Merge the structures
mergedStruct = struct1;
mergedStruct = cat(1, mergedStruct, struct2);
end
Categories
Find more on Structures 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!