필터 지우기
필터 지우기

Can anyone explain me what this line do strcmp(D(i).name,'..')?

조회 수: 4 (최근 30일)
Vinay Kumar
Vinay Kumar 2014년 12월 8일
댓글: Vinay Kumar 2014년 12월 8일
I am trying to read the number of images in a folder the following code. Can anyone explain this?
if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
imgcount = imgcount + 1; % Number of all images in the training database
end

채택된 답변

Guillaume
Guillaume 2014년 12월 8일
The code is obviously meant to be in a loop and the D most likely originates from a dir command. Its purpose is to count the number of files in the directory other than 'thumbs.db' and the '.' and '..' directories that matlab stupidly returns.
It assumes that all files in the directory are images and that there are no subdirectories. Otherwise imgcount will be wrong.
It's also not very well written. There is no need for a loop. You could replace:
for i = 1:numel(D) %I assume that's what it looks like
if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
imgcount = imgcount + 1; % Number of all images in the training database
end
end
with:
filenames = {D.name};
imgcount = sum(~strcmp(filenames, '.') & ~strcmp(filenames, '..') & ~strcmp(filenames, 'thumbs.db'));

추가 답변 (1개)

Henrik
Henrik 2014년 12월 8일
strcmp(D(i).name,'.'
this compares the string in D(i).name to '.'. If the string is indeed '.', it returns TRUE (in MATLAB, this is the same as 1).
Similarly for the two other strcmp: if the string is equal to '..' or 'Thumbs.db' it returns true.
The | between the three checks means either. So
strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db')
is true if the string in D(i).name is either '.', '..' or 'Thumbs.db'.
The not in front of all this means that imcount will increase by 1 if the string in D(i).name is neither '.', '..' nor 'Thumbs.db'
  댓글 수: 3
Henrik
Henrik 2014년 12월 8일
So the imgcounter is actually just counting the number of files in the directory?
Vinay Kumar
Vinay Kumar 2014년 12월 8일
Yes.. Thank you..

댓글을 달려면 로그인하십시오.

카테고리

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