How do I concatenate the fields of a struct containing timetables into a single timetable in MATLAB R2025a?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 16 Déc 2025 à 0:00
Réponse apportée : MathWorks Support Team
le 16 Déc 2025 à 18:58
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?
Réponse acceptée
MathWorks Support Team
le 16 Déc 2025 à 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Structures dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!