I should note the size of the different structs varies. I have never made a structure with substructs so I am not sure if this is okay, but the sizes are 1x1,1x1,1x9,1x13 and they all have 4 fields of data.
Combining structs into a larger struct?
8 views (last 30 days)
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
Stephen23
on 20 Jun 2017
Edited: Stephen23
on 20 Jun 2017
"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
% 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
0 Comments
See Also
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!