How do I concatenate the fields of a struct containing timetables into a single timetable in MATLAB R2025a?

13 次查看(过去 30 天)
I have a struct "structOfTimetables" in which every field contains a timetable. I use the following code to create it:
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed);
TT2 = TT;
TT3 = TT;
TT2.Properties.VariableNames = cellfun(@(x) [x, '2'], TT2.Properties.VariableNames, 'UniformOutput', false);
TT3.Properties.VariableNames = cellfun(@(x) [x, '3'], TT3.Properties.VariableNames, 'UniformOutput', false);
structOfTimetables = struct('TT1', TT, 'TT2', TT2, 'TT3', TT3);
How do I concatenate the timetables contained in the fields of the "structOfTimetables" struct into a single timetable?

采纳的回答

MathWorks Support Team
MathWorks Support Team 2025-12-16,0:00
The easiest way to concatenate the fields is to first convert your struct to a cell array, and then concatenate the cell array elements as follows. In this case, "horzcat" is used because all timetables have the same number of rows.
temp = struct2cell(structOfTimetables);
singleTimetable = horzcat(temp{:});
Note that in order for the above code to work, the variable names of the timetables being concatenated must be unique.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by