why I cannot list specify files with function dir
Show older comments
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 Comments
"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
on 9 May 2018
Guillaume
on 9 May 2018
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
on 9 May 2018
Guillaume
on 9 May 2018
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
on 9 May 2018
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
on 9 May 2018
Guillaume
on 9 May 2018
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
on 9 May 2018
Accepted Answer
More Answers (1)
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


