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.
조회 수: 73 (최근 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.
댓글 수: 0
답변 (2개)
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));
댓글 수: 0
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
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 Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!