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

1 回表示 (過去 30 日間)
Alberto Acri
Alberto Acri 2022 年 11 月 25 日
回答済み: Image Analyst 2022 年 11 月 25 日
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 Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by