saving files in .mat in another folder

3 views (last 30 days)
Giuseppe Anastasio
Giuseppe Anastasio on 7 Jan 2022
Edited: Kevin Holly on 7 Jan 2022
Hello
I have 30 files with this name in a folder: IM0, IM1 and so on. I want to save them with their corrispective names in another folder in mat files. How can I do?
Thanks

Answers (1)

Kevin Holly
Kevin Holly on 7 Jan 2022
Edited: Kevin Holly on 7 Jan 2022
I am going to assume that you are reading image files and then trying to save them in another folder as mat files.
%Select folder containing files
uiwait(msgbox('Select folder you want to save','Select folder'))
folder = uigetdir; %Alternatively, you can insert the filepath here
files = dir(fullfile(folder,'IM*'))
%Assuming those are image files you are reading, you can read the first one
%as shown below
variable = imread(fullfile(folder,files(1).name))
%Select directory where you want to save files.
uiwait(msgbox('Select folder where you want to save files','Select Save Directory'))
savedir = uigetdir; %Alternatively, you can insert the filepath here
%If you want to save the first file
save(fullfile(savedir,files(1).name),variable)
%You can use a for loop to go through the different files
for i=1:length(files)
save(fullfile(savedir,files(i).name,variable)
end
%load and saving together assuming those are image files
for i=1:length(files)
variable = imread(fullfile(folder,files(i).name))
save(fullfile(savedir,files(i).name,variable)
end

Community Treasure Hunt

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

Start Hunting!