Copy an image (to be selected with a parameter) from one folder to another

1 次查看(过去 30 天)
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);

采纳的回答

Florian Bidaud
Florian Bidaud 2022-11-25
编辑:Florian Bidaud 2022-11-25
filePattern = fullfile(origin_folder, ['name of figure' num2str(k) '.jpg']);
  2 个评论
Alberto Acri
Alberto Acri 2022-11-25
No, that's not what I was asking.
I want to copy a given .jpg image from the 'origin_folder' folder to the 'destdirectory ' folder using the 'k' parameter.
For example if I place k=1 I am going to copy the first image. If I put k=22 I am going to copy the 22nd image.
Florian Bidaud
Florian Bidaud 2022-11-25
编辑:Florian Bidaud 2022-11-25
Okay then use
listing = dir(origin_folder)
Then you will have the content of the folder.
This command will give you all the names of the pictures for example :
listing.name
That one will give you the date of each image :
listing.date
With that you can give a number to each image for example sorting them by date.
And you should be able to do what you want :)
Some information about dir function here : https://uk.mathworks.com/help/matlab/ref/dir.html
For example, you can create a list of your image names and a list of your image dates:
k = 1;
for i = 1:length(listing)
if ~listing(i).isdir
imageNames{k} = listing(i).name;
imageDates{k} = listing(i).date;
k = k+1;
end
end
Then you could sort them by date :
imageDates = datetime(imageDates,'Format','dd-MMM-yyyy HH:mm:SS')
[sortedDates,I] = sort(imageDates)
sortedNames = imageNames(I);
And now you have acess to your images names with k variable sorted by dates, i.e. k = 1 will be the oldest and k = length(sortedNames) will be the newest.
then your line would become
filePattern = fullfile(origin_folder, sortedNames{k});

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-11-25
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);

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by