How can I merge two structs?

조회 수: 19 (최근 30일)
Sander Janssen
Sander Janssen . 2022년 10월 11일
댓글: Sander Janssen . 2022년 10월 11일
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 (char) and (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
Any help is greatly appreciated, thanks in advance!
  댓글 수: 2
Sander Janssen
Sander Janssen 2022년 10월 11일
I have made an example structure that looks the same (just different names/values).

댓글을 달려면 로그인하십시오.

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 10월 11일
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
Sander Janssen
Sander Janssen 2022년 10월 11일
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...

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by