How do I find a file name containing a particular string in a given directory and read this present file and write into other folder.

240 visualizaciones (últimos 30 días)
i have folder in this different .jpg files are present with specified name. i want to read file with particular name not fullfile name .
like in folder i have AKS_9129.jpg file present but i want to read with 9129.jpg name . not include AKS_9129.jpg
path15 = strcat('E:\folder\',num2str(9129),'.jpg');
im15 = imread(path15);
error occured file is not found.
i have enclosed folder structure.

Respuestas (2)

Amirali Kamalian
Amirali Kamalian el 21 de Ag. de 2019
You can make the code simpler by replacing this line
X = ~cellfun('isempty',strfind(N,num2str(9129)));
with
X = contains(N,num2str(9129));

Stephen23
Stephen23 el 27 de Sept. de 2017
Editada: Stephen23 el 27 de Sept. de 2017
Method one: strfind:
P = 'E:\folder';
S = dir(fullfile(P,'*.jpg'));
N = {S.name};
X = ~cellfun('isempty',strfind(N,num2str(9129)));
I = imread(fullfile(P,N{X}))
Method two: dir:
P = 'E\folder';
F = sprintf('*%i*.jpg',9129);
S = dir(fullfile(P,F));
N = S.name;
I = imread(fullfile(P,N));
Note that in both cases you will need to check how many matching filenames there are. You should also read the MATLAB documentation:
and this forum, e.g.:
  2 comentarios
praveen chandaliya
praveen chandaliya el 27 de Sept. de 2017
IF TWO FILE MATCH THAN THIS ERROR X =
1 0 0 0 1 0 0 0 0 0 0 0
Error using imread (line 349) File "E:\actualdata\9129.JPG\AKS_9129.jpg" does not exist.
Error in folderread (line 10) I = imread(fullfile(P,N{X})); HOW TO SOLVE THIS ERROR
Stephen23
Stephen23 el 27 de Sept. de 2017
Editada: Stephen23 el 27 de Sept. de 2017
@praveen chandaliya: if two or more files match that number then you have to decide what to do. You might want to process both/all of them, or throw an error, or ignore them. How am I supposed to know what you want to do in that situation?
In any case I would highly recommend that you read the link to the documentation that I gave. The method you are trying to attempt is rather strange, and you would likely be much better off using one of the more standard ways to process a sequence of files.
The error message that you show tells me that you have used some different code than what I showed you, because in your error message 9129.JPG appears twice (did you read the message?):
E:\actualdata\9129.JPG\AKS_9129.jpg
My code does not do that, so you need to fix your code.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by