How to import multiple .mat files into the same workspace
Show older comments
Hello,
I have multiple .mat files that contain different arrays.
These arrays have the same name in each .mat file; I need to open two or more .mat files to elaborate the data.
The problem is that the content of the most recent .mat file overwrites the previous one since the name of the arrays is always the same.
For example, let's say I have test1.mat that contains:
accel
effort
gyro
and test2.mat that contains:
accel
effort
gyro
Is there a way to be able to load into the workspace the arrays coming from test1 and test2 at the same time?
2 Comments
"Unfortunately, these mat files are generated by an external software system that run on a robot, so I cannot change the variables name."
Why do you write "unfortunately" ? Neither of us asked you to change the variable names.You seem imagine that to be a problem, but it isn't. In fact, keeping the variable names unchanged is by far the best data design.
"do you think it is possible to automatize the loading of each mat file even in this case where there are different directories nested in the current directory?"
Of course, see the comment under my answer.
Accepted Answer
More Answers (1)
Walter Roberson
on 24 Jan 2025
projectdir = '.'; %path to .mat files
dinfo = dir( fullfile(projectdir, '*.mat') );
data = struct();
for K = 1 : length(dinfo)
fullname = fullfile( dinfo(K).folder, dinfo(K).name );
[~, basename, ~] = fileparts(fullname);
data.(basename) = load(fullname);
end
The result will be a structure that has one field for each file loaded, with the field named after the file. Each field will be a struct that contains one field for each loaded variable.
So data.test1.accel, data.test1.effort, data.test2.accel, data.test2.effort
It is common that a different organization of data is desireable, such as
projectdir = '.'; %path to .mat files
dinfo = dir( fullfile(projectdir, '*.mat') );
filenames = fullfile( {dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
accels = cell(numfiles,1);
efforts = cell(numfiles,1);
gyros = cell(numfiles,1);
for K = 1 : numfiles
data = load(filenames{K});
accels{K} = data.accel;
efforts{K} = data.accel;
gyros{K} = data.gyro;
end
This gives cell arrays accels efforts gyros containing the entries; the filenames can be pulled out of filenames
Thirdly, it is not uncommon for the best organization data to be multidimensional arrays.
projectdir = '.'; %path to .mat files
dinfo = dir( fullfile(projectdir, '*.mat') );
filenames = fullfile( {dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
for K = numfiles : -1 : 1 %count backwards
data = load(filenames{K});
accels(:,:,K) = data.accel;
efforts(:,:,K) = data.effort;
gyros(:,:,K) = data.gyro;
end
This assumes the accels and efforts and gyros are vectors or 2d arrays of consistent size, and splices the information of the various files together into 3D arrays.
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!