Merging MATLAB Structure

I have the following structure

data = id: [143x1 double] datenum: [143x1 double] Timestamp: {143x1 cell} Min_F1_USA_40__u: [143x1 double] Max_F1_USA_40__u: [143x1 double] Mean_F1_USA_40__u: [143x1 double] Stddev_F1_USA_40__u: [143x1 double] MeanVals_F1_USA_40__u: [143x1 double] a0_F1_USA_40__u: [143x1 double] a1_F1_USA_40__u: [143x1 double] a2_F1_USA_40__u: [143x1 double] a3_F1_USA_40__u: [143x1 double] a4_F1_USA_40__u: [143x1 double] 

So, I have more than 50 fields in the structure

I have another structure 3 with the same structure and I want to combine this structure

When I have 3 structures, I get the following structure

 data = id: [429x1 double] datenum: [429x1 double] Timestamp: {429x1 cell} Min_F1_USA_40__u: [429x1 double] Max_F1_USA_40__u: [429x1 double] Mean_F1_USA_40__u: [429x1 double] Stddev_F1_USA_40__u: [429x1 double] . . . 
+4
source share
2 answers

Sorry, I misunderstood your question - this is the second attempt.

There may be an easier way, but you can get a list of all the fields in data using mynames=fieldnames(data) . You can then iterate over them all and assign them to the overall structure as follows:

 combineddata.(mynames{i})=[data1.(mynames{i}); data2.(mynames{i}); data3.(mynames{i})]; 
+3
source

Here is one solution using the FIELDNAMES , CELLFUN, and CELL2STRUCT functions :

 data = [data1 data2 data3 data4]; %# Create a structure array of your data names = fieldnames(data); %# Get the field names cellData = cellfun(@(f) {vertcat(data.(f))},names); %# Collect field data into %# a cell array data = cell2struct(cellData,names); %# Convert the cell array into a structure 
+2
source

All Articles