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

6 views (last 30 days)
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?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 16 Dec 2025 at 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.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!