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

5 views (last 30 days)
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}];

Accepted Answer

Stephen23
Stephen23 on 7 Aug 2023
Edited: Stephen23 on 7 Aug 2023
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 Comments
Alberto Acri
Alberto Acri on 8 Aug 2023
Edited: Alberto Acri on 8 Aug 2023
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 on 8 Aug 2023
"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

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!