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.

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

回答(2 个)

Amirali Kamalian
Amirali Kamalian 2019-8-21
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 2017-9-27
编辑:Stephen23 2017-9-27
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 个评论
praveen chandaliya
praveen chandaliya 2017-9-27
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 2017-9-27
编辑:Stephen23 2017-9-27
@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.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by