How to simply concatenate two structures with different fields in Matlab?

12,543

I got what I want by using the suggestion in update struct via another struct in Matlab, as follows:

names = [fieldnames(struct1); fieldnames(struct2)];
struct3 = cell2struct([struct2cell(struct1); struct2cell(struct2)], names, 1);
Share:
12,543
Barpa
Author by

Barpa

Updated on June 04, 2022

Comments

  • Barpa
    Barpa almost 2 years

    Is there any simple way to combine the following two structures, without using for loop or CELLFUN?

    struct1 = 
    
        a: {43x1 cell}
    
    struct2 = 
    
        b: [43x1 double]
        c: {43x1 cell}
    

    I would like to have the combined structure like this:

    struct3 = 
    
        a: {43x1 cell}
        b: [43x1 double]
        c: {43x1 cell}