open some .mat files (whose names are saved inside a cell)

2 次查看(过去 30 天)
Hi! I need to open some .mat files (whose names are saved inside the 'name_cell' cell).
I tried this way but, of course, it saves them all with the same name, overlapping them.
load name_cell.mat
folder = pwd;
for K = 1:height(name)
% Load file .mat
file = load(fullfile(folder,name{K,1}));
file = file.original_name; % all files are originally named 'original_name'
end
I would like to open them all with that specific name inside 'name_cell'.
Also, I need to save those open files in a single cell, like this:
matrix_cell = [{file_1};{file_2}];

采纳的回答

Stephen23
Stephen23 2023-8-7
编辑:Stephen23 2023-8-7
Where C is your cell array of filenames, and assuming exactly one array is saved in each MAT file:
D = C;
for k = 1:numel(C)
S = load(C{k});
D{k} = S.original_name;
end
  4 个评论
Alberto Acri
Alberto Acri 2023-8-8
编辑:Alberto Acri 2023-8-8
Hi @Stephen23. I tried the code again and it works with the preceding code:
D = C;
for k = 1:numel(C)
S = load(C{k},'original_name');
D(k) = struct2cell(S);
end
I did something wrong in copying and pasting. But I need to read the .mat files from a 'folder' other than pwd. How can I modify the code you gave me?
Stephen23
Stephen23 2023-8-8
"But I need to read the .mat files from a 'folder' other than pwd."
Provide the absolute/relative filepath and use FULLFILE:
P = 'absolute or relative filepath to where the files are saved';
D = C;
for k = 1:numel(C)
F = fullfile(P,C{k});
S = load(F,'original_name');
D(k) = struct2cell(S);
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by