How to import multiple .mat files into the same workspace

31 ビュー (過去 30 日間)
Mark
Mark 2025 年 1 月 24 日 22:22
コメント済み: Mark 2025 年 1 月 26 日 23:03
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 件のコメント
Mark
Mark 2025 年 1 月 25 日 7:06
移動済み: Stephen23 2025 年 1 月 25 日 7:32
Thank you a lot, guys, for your support!
Unfortunately, these mat files are generated by an external software system that run on a robot, so I cannot change the variables name.
I have something like this:
current_folder/folder1/test1.mat
current_folder/folder1/test2.mat
current_folder/folder1/test3.mat
current_folder/folder2/test1.mat
current_folder/folder2/test2.mat
current_folder/folder2/test3.mat
current_folder/folder3/test1.mat
current_folder/folder3/test2.mat
current_folder/folder3/test3.mat
..
and I run my matlab script from
current_folder
.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?
I'm sorry if this is a stupid question, I'm not very used to Matlab, unfortunately.
Stephen23
Stephen23 2025 年 1 月 25 日 7:33
編集済み: Stephen23 2025 年 1 月 25 日 7:38
"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.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 1 月 25 日 6:39
編集済み: Stephen23 2025 年 1 月 25 日 6:53
"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."
Having exactly the same variable names in each MAT file is the opposite of a problem, it is the best and reccommended way of designing data in multiple MAT files, because it allows you to write robust and efficient code to process it. Whatever you do, do not try to LOAD all of those variables directly into the workspace... there be dragons!
The most important change is to always LOAD into an output variable (which is a scalar structure):
S = load(..);
What to do then depends rather on the nature of that data and what you are going to do with it. But here is a general approach:
P = '.'; % absolute or relative path to where the MAT files are saved.
S = dir(fullfile(P,'test*.mat'));
S = natsortfiles(S); % optional, must be downloaded, see link below.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
S(k).data = load(F);
end
D = [S.data]; % optional
After that all of the imported data will be in the non-scalar structure S. You can easily access it using indexing, e.g: the 2nd file:
S(2).name % filename
S(2).data % complete imported data
D(2).accel
D(2).effort
D(2).gyro
NATSORTFILES can be used to sort the files into alphanumeric order, it can be downloaded here:
See also:
  2 件のコメント
Stephen23
Stephen23 2025 年 1 月 25 日 7:35
編集済み: Stephen23 2025 年 1 月 25 日 7:40
Given the folder/file structure shown in your comment here, you could easily do something like this:
P = '.'; % absolute or relative path to the parent folder.
S = dir(fullfile(P,'*','*.mat'));
S = natsortfiles(S); % optional, must be downloaded, see link in answer.
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = load(F);
end
D = [S.data]; % optional
Accessing the data is exactly as I showed in my answer. You can easily access it using indexing, e.g: the 2nd file:
S(2).name % filename
S(2).folder % folder where that file is saved
S(2).data % complete imported data
D(2).accel
D(2).effort
D(2).gyro
Mark
Mark 2025 年 1 月 26 日 23:03
Thank you a lot! It works perfectly!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 1 月 24 日 23:45
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.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by