How can I merge two structs?

My question might become a bit complicated, so please let me know if I have not explained something well.
I have got a structure that has two relevant fields, one field is a character which shows the date and the other field is a structure with data, let's call them A.date (char) and A.data (struct).
Some of the dates are duplicate (on purpose) I would like to add the values from the structure of those duplicates to the structure with the same date (so effectively merging the structures).
Sketch of what I'd like to achieve:
for i=1:length(A)
if A(i).date == A(i+1).date % Will work untill final value is reached
A(i).data + A(i+1).data % Won't work, but I would want to merge A(i).data + A(i+1).data
end
end
Any help is greatly appreciated, thanks in advance!

2 Comments

Can you please share a file for the struc A?
I have made an example structure that looks the same (just different names/values).

Sign in to comment.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 11 Oct 2022
Due to the need to merge it might be easiest to use struct2table, table2timetable, make sure it is merged well as a time table, then table2struct to change it back to the struct data type.

7 Comments

This almost works to the extent of what I want. It will add the values from A(i+1).data but will not delete the now useless A(i+1), plus it removes A.date which ideally shouldn't happen.
Is there a way to 'fix' these problems?
Can you attach your code for review?
The code I used is:
tbl = struct2table(A)
tbl.date = datetime(tbl.date, 'InputFormat', 'yyyyMMdd') % Because A.date class was not correct
timetbl = table2timetable(tbl)
new_struct = table2struct(timetbl)
Sadly I cannot share my actual structure A, but there is an example attached in my original question.
Your example seems to imply that A(1) and A(2) should be merged since they have the same date field.
» A([1 2]).data
ans =
struct with fields:
a: 'something'
b: 'a number'
c: 'another value'
d: 10
ans =
struct with fields:
e: 'something else'
f: 'a different number'
g: 'another value'
h: 10
What does it mean to "merge" these two? You want the new A(1) to contain fields a, b, c, d, e, f, g, h from the original A(1) and A(2)?
Yes, I'd like A(1) to contain fields a,b,c,d,e,f. I'd also want it to still have the date field. Ideally A(2) would now have value 2 for date.
If you're familiar with Python, what I'm trying to achieve here is somewhat similar to the pd.concat function from Pandas.
Benjamin Thompson
Benjamin Thompson on 11 Oct 2022
Edited: Benjamin Thompson on 11 Oct 2022
If you don't know the names of the fields in the two structures you are merging then it gets awkward. This is for copying one field of A2.data over to A1.data. You can generalize this into a loop that runs length(fields) times.
» fields = fieldnames(A(2).data);
» A(1).data = setfield(A(1).data,fields{1},getfield(A(2).data,fields{1}))
A =
1x12 struct array with fields:
date
data
» A(1).data
ans =
struct with fields:
a: 'something'
b: 'a number'
c: 'another value'
d: 10
e: 'something else'
Then to delete A(2):
» A(2) = []
A =
1x11 struct array with fields:
date
data
Now if you know the fields of A1.data and A2.data:
» A(1).data = struct('a', A(1).data.a,'b', A(1).data.b,'c', A(1).data.c,'d', A(1).data.d, 'e', A(2).data.e, 'f', A(2).data.f, 'g', A(2).data.g, 'h', A(2).data.h)
A =
1x12 struct array with fields:
date
data
» A(1).data
ans =
struct with fields:
a: 'something'
b: 'a number'
c: 'another value'
d: 10
e: 'something else'
f: 'a different number'
g: 'another value'
h: 10
The problem is that I do not know which row will be a duplicate, so I'd first need to identify that. I might be able to add them beforehand but I'll have to check that...

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 11 Oct 2022

Commented:

on 11 Oct 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!