Why dir function is creating additional files while creating a structure? how to ignore it?

17 ビュー (過去 30 日間)
Im trying to make a loop that reading all the files in a choosen folder, I use the dir function to make list of the names of the files and to iterate this list:
files = dir('folder');
for k =1:length(files)
I = imread(files(k).name);
But there is always an error:
Error using imread (line 347)
Cannot open file "." for reading. You might not have read permission.
When I open my structure named "files" there is 2 new rows that the dir function creates in addition to all the other files in that structure:
the first file named '.' and the second '..', those files not exist in the original folder, what to do to erase them or ignore them permanently?

採用された回答

Catalytic
Catalytic 2019 年 3 月 27 日
編集済み: Catalytic 2019 年 3 月 27 日
Do a more specific search, like
files = dir('folder/*.jpg');
Or, just throw the first 2 away
files = dir('folder');
files(1:2)=[];
  2 件のコメント
Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 28 日
"Or, just throw the first 2 away"
Do NOT do this. The files/folders are those returned by the OS, and the presence or specific location of . and .. is not guaranteed. This has been discussed many times on this forum. It is also easy to demonstrate: simply create some files with names starting with ! or ' (both perfectly valid characters for filenames on both Windows and Unix, and which sort before the . character), and you will see that . and .. are NOT the first names returned by dir:
>> mkdir('subdir')
>> fclose(fopen('subdir/!test.txt','wt'));
>> fclose(fopen('subdir/''test.txt','wt'));
>> fclose(fopen('subdir/_test.txt','wt'));
>> S = dir('subdir');
>> {S.name}
ans =
'!test.txt' ''test.txt' '.' '..' '_test.txt'
To write robust code, simply use setdiff or ismember to select or remove names:
>> setdiff({S.name},{'.','..'})
ans =
'!test.txt' ''test.txt' '_test.txt'
Matt J
Matt J 2019 年 3 月 28 日
編集済み: Matt J 2019 年 3 月 28 日
Or, similar to what Stephen suggests,
Names=deblank(string(ls('folder')));
Names(Names=="." | Names=="..")=[];
for I =1:numel(Names)
I = imread(Names{i});
...
end

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2019 年 3 月 28 日
If you're calling imread on the output of dir, you probably want to first check that the entry you're trying to read isn't a directory. You can do this all at once using the isdir field of the struct array returned by dir.
D = dir();
numberOfEntriesPreTrim = numel(D);
D = D(~[D.isdir]);
numberOfEntriesPostTrim = numel(D);
numberOfDirectories = numberOfEntriesPreTrim-numberOfEntriesPostTrim;
for whichfile = 1:numberOfEntriesPostTrim
fprintf('File %d is %s.\n', whichfile, D(whichfile).name)
end
fprintf('Printed %d of %d entries in pwd (%d are directories.)\n', ...
numberOfEntriesPostTrim, numberOfEntriesPreTrim, numberOfDirectories);
Note that '.' and '..' are not printed by the fprintf statement. They were trimmed because they're directories, but they still count for the last fprintf statement.
I needed to wrap D.isdir in square brackets because it creates a comma-separated list. The square brackets concatenates all the elements of that list into a vector.
As for what . and .. are, on the operating systems on which MATLAB is currently supported they are the current directory and the parent directory, respectively.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by