why I cannot list specify files with function dir
이전 댓글 표시
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
"If I call dir function as following:"
dir
"it will return all files"
Not at all: it does not return anything, it simply lists them in the command window. Have a look in your workspace, and you won't find any variable being returned by dir. This is the difference between a function returning an output, and a function displaying something in the command window, which are are two totally different things. Every instance of dir being called without an output argument simply displays/lists something in the command window, and nothing is returned. If you want to assign its output to an argument then this output will be a structure, as you have already observed.
li yan
2018년 5월 9일
Guillaume
2018년 5월 9일
Using the same directory as in your screenshot what is the output of:
s = dir;
double(s(10).name(end-4:end)) %extension of arbitrary file number 10
li yan
2018년 5월 9일
Guillaume
2018년 5월 9일
This is indeed very odd.
Which version of matlab are you using? Looks like an old version from your help screenshot.
What is the output of:
s = dir;
jpgfiles = s(~cellfun(@isempty, regexp({s.name}, '\.jpg$')));
numel(jpgfiles)
Guillaume
2018년 5월 9일
Actually, when you sow the output of
>>dir
can you show the entire list of files? Is there actually a file that looks like *.jpg
li yan
2018년 5월 9일
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
2018년 5월 9일
채택된 답변
추가 답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



