Combining Similar Time Arrays into a Single Updated Time Array

조회 수: 8 (최근 30일)
David
David 2025년 4월 24일
댓글: Star Strider 2025년 4월 25일
I have two structures: Structure A and Structure B.
Both structures have 130 field variables. These variables are time/values on two different DAQs. Structure A uses DAQ A. Structure B uses DAQ B. As such, they have similar but not the same outputs. Sometimes Structure A Field variables have a time/value drop out while Structure B field variables do not.
How do I combine Field Variable 1 from Structure A and Structure B into a single field variable into structure C without any time/value repeats? Then repeat this process for all of the variables.
Assumption: The data values are the same value at the same time even if the indices do not line up. They end at the same time, but are not the same sizes.
Inputs:
StructureA.FieldVariable1.Time
StructureA.FieldVariable1.Value
StructureB.FieldVariable1.Time;
StructureB.FieldVariable1.Value
Output:
StructureC.FieldVariable1.Time
StructureC.FieldVariable1.Value
Ex:
FROM:
Structure A Field Variable 1 Structure B Field Variable 1
Time Data Time Data
1 0.1 2 0.2
2 0.2 3 0.3
4 0.4 5 0.5
5 0.5 6 0.6
6 0.6 7 0.7
8 0.8 8 0.8
10 1.0 9 0.9
Left blank on purpose 10 1.0
Structure C Field Variable 1
Time Data
1 0.1 (Filled in from Structure A)
2 0.2 (same value so filled in from either structure)
3 0.3 (Filled in from Structure B)
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1.0

답변 (1개)

Star Strider
Star Strider 2025년 4월 24일
There are some core MATTLAB approaches for this, depending on the result you want.
You would have to convert them to timetable arrays, and after that you can use either retime or synchronize depending on the result you want. I am not sufficiently clear on that to suggest a specific apprroach.
Example —
AFV_1 = table(seconds([1;2;4;5;6;8;10]),[1;2;4;5;6;8;10]/10, VariableNames=["Time","Data"]);
BFV_1 = table(seconds([2;3;4;5;6;7;8;9;10]),[2;3;4;5;6;7;8;9;10]/10, VariableNames=["Time","Data"]);
AFV_1TT = table2timetable(AFV_1)
AFV_1TT = 7x1 timetable
Time Data ______ ____ 1 sec 0.1 2 sec 0.2 4 sec 0.4 5 sec 0.5 6 sec 0.6 8 sec 0.8 10 sec 1
BFV_1TT = table2timetable(BFV_1)
BFV_1TT = 9x1 timetable
Time Data ______ ____ 2 sec 0.2 3 sec 0.3 4 sec 0.4 5 sec 0.5 6 sec 0.6 7 sec 0.7 8 sec 0.8 9 sec 0.9 10 sec 1
CFV_TT = synchronize(AFV_1TT, BFV_1TT)
CFV_TT = 10x2 timetable
Time Data_AFV_1TT Data_BFV_1TT ______ ____________ ____________ 1 sec 0.1 NaN 2 sec 0.2 0.2 3 sec NaN 0.3 4 sec 0.4 0.4 5 sec 0.5 0.5 6 sec 0.6 0.6 7 sec NaN 0.7 8 sec 0.8 0.8 9 sec NaN 0.9 10 sec 1 1
CFV_TT2 = table;
CFV_TT2.Time = CFV_TT.Time;
CFV_TT2.Data = mean(CFV_TT{:,[1 2]}, 2, 'omitnan')
CFV_TT2 = 10x2 table
Time Data ______ ____ 1 sec 0.1 2 sec 0.2 3 sec 0.3 4 sec 0.4 5 sec 0.5 6 sec 0.6 7 sec 0.7 8 sec 0.8 9 sec 0.9 10 sec 1
Make appropriate changes to get the result you want.
.
  댓글 수: 2
David
David 2025년 4월 25일
Appreciate the insight. It helps to have a starting point and functions i can look at. I ran into the problem of my time data being in the microsecond regime but matlab only seems to go down to milliseconds.
I would also have to do checks on the other data to ensure it matches with the current data before importing the data to fill in the gaps. I don't think missing 0.2s here or there is significant in the grand scheme of 400 seconds. It's a nice to have, but the amount of effort required versus benefit gained isn't there, and I need to spend my time elsewhere.
I think at this point, I'm just going to check which is the longer array, use that, and move on. Thanks!
Star Strider
Star Strider 2025년 4월 25일
My pleasure!
The datetime function can do microseconds. You have to specify the seconds field as ss.SSSSSS with s denoting seconds and the S denoting fractions of a second.
Example —
T = datetime(2025, 04, 25, 10, 22, 42.123456, Format='dd-MMM-yyyy HH:mm:ss.SSSSSS')
T = datetime
25-Apr-2025 10:22:42.123455
format longG
s = second(T)
s =
42.123456
Note that the microseconds are preserved, even if they aren't all displayed correctly in the original datetime call.
I'm not sure what other problems you may be having, however datetime is a powerful function and the timetable functions can make your tasks immeasurably easier.
.

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by