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

1 visualización (últimos 30 días)
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);

Respuesta aceptada

Florian Bidaud
Florian Bidaud el 25 de Nov. de 2022
Editada: Florian Bidaud el 25 de Nov. de 2022
filePattern = fullfile(origin_folder, ['name of figure' num2str(k) '.jpg']);
  2 comentarios
Alberto Acri
Alberto Acri el 25 de Nov. de 2022
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 el 25 de Nov. de 2022
Editada: Florian Bidaud el 25 de Nov. de 2022
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});

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 25 de Nov. de 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);

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by