why I cannot list specify files with function dir
Mostra commenti meno recenti
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 Commenti
"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
il 9 Mag 2018
Guillaume
il 9 Mag 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
il 9 Mag 2018
Guillaume
il 9 Mag 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
il 9 Mag 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
il 9 Mag 2018
Guillaume
il 9 Mag 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
il 9 Mag 2018
Risposta accettata
Più risposte (1)
KSSV
il 9 Mag 2018
images = dir('*.jpg') ;
iwant = {images(:).name}' ;
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



