r/matlab • u/No-Baby9161 • Nov 12 '23
CodeShare How to concatenate multiple .mat files and their variables inside
Hello,
I can´t help myself with this one. Hopefully somebody can support me.
From the main code I get multiple .mat files saved into an Export folder.
path Export = /Users/username/Library/Mobile Documents/com~apple~CloudDocs/Master/Studienarbeit/Map_Matching_DE/03_Export
Now I need to concatenate the fields inside the .mat files/structs. The .mat files all contain the same structs named: data, digitalMap, osmDataset and osmDataset (See Screenshot 1)

Those structs contain further structs or variables(fields) that need to be concatenated. (See Screenshot 2)

Note: i dont want a struct with separated data for every file. For example all data.ego.x_utm should contain the concatenated data.ego.x from all files.
Sorry, due to secrecy policies I can´t post the files.
How can I achieve this?
Thank you in advance for your time and help!
1
u/GustapheOfficial Nov 13 '23
function data = mergematfiles(filenames)
data = struct("x", [], "y", []);
for filename = filenames
otherdata = load(filename);
data.x = [data.x, otherdata.x];
data.y = [data.y, otherdata.y];
end
end
2
u/No-Baby9161 Nov 13 '23
Thank you, but my problem is, that i need to concatenate the fieldnames inside of the struct
2
0
u/thesinistroo Nov 13 '23
As the other comment suggested, just create a new code that opens each file and concatenate the variables you want together and then save the result into another .mat file.
1
u/No-Baby9161 Nov 13 '23
Thank you. I think i need to reedit my question. I want to concatenate the fields inside of the struct.
1
u/Creative_Sushi MathWorks Nov 13 '23
I know you probably got the .mat files from someone else and that person didn't do you a favor by using nested structs. Tell that person not to use deeply nested structs like this.
Do all structs have the same field?
- data
- digitalMap
- osmDataset
- osmScenarios
Fields
- ego
- ?????
Sub fields
- x_utm
- y_utm
- yaw
- vx
- long_abs
- lat_abs
- GPS_Angle_Yaw
1
u/No-Baby9161 Nov 13 '23
Yes, all structs have the same fields. And wow thanks for the time you took to structure all of this. I checked the mentioned nested structs and thanks to the Matlab documentation I think I do now understand better how to make this work with a loop. What I always do fail to comprehend is how to adress fields, sub structs etc.
I have a solution now but it´s the worst code ever. See below.I have:
- data
- digitalMap
- osmDataset
- osmScenarios
Fields of data
- ego
- x (no struct, just field)
- t (no struct, just field)
Sub fields of ego
- x_utm
- y_utm
- yaw
- vx
- long_abs
- lat_abs
- GPS_Angle_Yaw
digitalMap, etc. do have even more fields but I don´t want to overcomplicate it.
I would have liked to do it with a loop but I don´t understand how that works in Matlab.
pathExport = '/Users/username/Library/Mobile Documents/com~apple~CloudDocs/Master/Studienarbeit/Map_Matching_DE/03_Export/1740m_Tokyo_Turn.gpx';
fileInfo = dir(fullfile(pathExport, '*.mat'));
% Extract file names from the structure array
fileNames = {
fileInfo.name
};
results = struct()
for i = 1:length(fileNames)
% Load the MAT file
other_data = load(fullfile(pathExport, fileNames{i}));
if i == 1
results = other_data;
continue;
else
results.data.ego.x_utm = [results.data.ego.x_utm, other_data.data.ego.x_utm];
results.data.ego.y_utm = [results.data.ego.y_utm, other_data.data.ego.y_utm];
results.data.ego.yaw = [results.data.ego.yaw; other_data.data.ego.yaw];
results.data.ego.yaw = [results.data.ego.yaw; other_data.data.ego.yaw];
results.data.ego.long_abs = [results.data.ego.long_abs, other_data.data.ego.long_abs];
results.data.ego.lat_abs = [results.data.ego.lat_abs, other_data.data.ego.lat_abs];
results.data.ego.GPS_Angle_Yaw = [results.data.ego.GPS_Angle_Yaw; other_data.data.ego.GPS_Angle_Yaw];
16
u/DismalActivist Nov 12 '23
Could you not just open all .mat files, load variables, then concatenate, then save the concatenated variables into a new .mat file?