Copy an image (to be selected with a parameter) from one folder to another
Show older comments
Hello! I would like to copy an image (to be selected with a parameter, for example 'k') from one 'origin_folder' folder to another 'destdirectory' folder.
Within the 'origin_folder' folder there are 20 .jpg images and I would like to identify one of them with parameter 'k' by modifying this code:
origin_folder = 'C:\Users\Alberto\Desktop\....';
destdirectory = 'C:\Users\Alberto\Download\....';
k = 1; % number of the figure I want to copy
filePattern = fullfile(origin_folder, 'name of figure.jpg'); % change this line taking into consideration 'k'
fileNamesToTransfer = dir(filePattern);
baseFileName = fileNamesToTransfer.name;
fullInputFileName = fullfile(origin_folder, baseFileName);
fullOutputFileName = fullfile(destdirectory, baseFileName);
copyfile(fullInputFileName, fullOutputFileName);
Accepted Answer
More Answers (1)
Image Analyst
on 25 Nov 2022
I guess you figured it out since you Accepted the other answer but this is what I'd do to get the 22nd image copied. The key thing is you have to index fileNamesToTransfer to specify the one you want.
origin_folder = 'C:\Users\Alberto\Desktop\....';
destdirectory = 'C:\Users\Alberto\Download\....';
k = 22; % number of the file I want to copy
filePattern = fullfile(origin_folder, '*.jpg'); % change this line taking into consideration 'k'
fileNamesToTransfer = dir(filePattern);
baseFileName = fileNamesToTransfer(k).name; % Specify the k'th file.
fullInputFileName = fullfile(origin_folder, baseFileName);
fullOutputFileName = fullfile(destdirectory, baseFileName);
copyfile(fullInputFileName, fullOutputFileName);
Categories
Find more on Convert Image Type 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!