필터 지우기
필터 지우기

why I cannot list specify files with function dir

조회 수: 4 (최근 30일)
li yan
li yan 2018년 5월 9일
편집: li yan 2018년 5월 10일
I want to list all special files with specified extensions, such as all *.jpg files. I cd the matlab to the current folder, and call the dir function as following:
dir *.jpg
it does not return all file names but a struct with name attribute and other fields, like the following picture showing
If I call dir function as following:
dir
it will return all files
I just want to list all jpg files, but not including the root directory, how to use dir function, please?
  댓글 수: 9
Guillaume
Guillaume 2018년 5월 9일
I wanted to see the end of the list of files. From the screenshot you show in KSSV answer, it looks like somehow you have a file actually called '*.jpg' in that directory. Something I didn't think was possible on Windows.
What is the output of
s = dir('*.jpg');
double(s.name)
li yan
li yan 2018년 5월 9일
It's really odd. I copy 3 pictures to another folder, and test like this:
s = dir('*.jpg');
it works well, it means that the variable s really stores 3 picture information as a struct. But if I copy all pictures to the folder, and re-test like above, it does not work well.
I'm sure that the folder does not contain a file named *.jpg.
last, I test dir in the current folder contains all files ( there is no file named *.jpg, I'm sure) using your code:
s = dir('*.jpg');
double(s.name)
the result is 42 46 106 112 103

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

채택된 답변

Jan
Jan 2018년 5월 9일
편집: Jan 2018년 5월 9일
Guillaume is right: You have created a file called '*.jpg'. This is not possible under Windows with standard tools, but you can do this under Linux or with using evil tricks. I suggest to start your machine from a Linux live CD or USB stick and delete the file '*.jpg'.
You can install the Linux subsystem: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc771470(v=ws.11) (here for Windows 7) and delete the file from there.
This works for a trailing dot in the file name, which is illegal also:
system('del \\?\D:\Temp\asd.')
I cannot test this with a * in the name, because I'm not able to create such a file...
A chkdsk might fix the file name also.
  댓글 수: 2
Guillaume
Guillaume 2018년 5월 9일
This is why I wanted a screenshot of the end of the file listing. Everything points to a file called *.jpg in the current directory.
How you would create such a file in Windows is a mystery. The WIN32 API won't let you. I suggest copying all the valid files into a new directory then deleting the current directory with the invalid *.jpg file (if the OS actually lets you).
li yan
li yan 2018년 5월 10일
편집: li yan 2018년 5월 10일
Thank you for your help very much.
I do not know why there is a *.jpg file in the folder, even though I cannt see that file from explorer.

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

추가 답변 (1개)

KSSV
KSSV 2018년 5월 9일
images = dir('*.jpg') ;
iwant = {images(:).name}' ;
  댓글 수: 1
li yan
li yan 2018년 5월 9일
it does not work correctly.
images = dir('*.jpg') ;
this just return a struct with one element, like this:

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

카테고리

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