How do I concatenate the fields of a struct containing timetables into a single timetable in MATLAB R2025a?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 16 de Dic. de 2025 a las 0:00
Respondida: MathWorks Support Team
el 16 de Dic. de 2025 a las 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?
Respuesta aceptada
MathWorks Support Team
el 16 de Dic. de 2025 a las 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!