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 commentaires

Stephen23
Stephen23 le 9 Mai 2018
Modifié(e) : Stephen23 le 9 Mai 2018
"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.
yes, I test the function of "dir" in command window. If I assign its outputs to an argument, like this:
s = dir;
the output s is a struct stores all file names and other attributes. But in my current folder, I just want to get all jpg file names, according to the help document, I write the code like this:
s = dir('*.jpg');
this does not return all jpg files to the argument s, this make me confused.
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
the result is
ans =
56 46 106 112 103
the following is my experimental code:
and the following is the help document, i'm confused that I cant get specified files using wildcards(*).
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)
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
li yan le 9 Mai 2018
My matlab version is Matlab2015b (8.6.0.267246). I type the code you put, the result is as following:
actually, my current folder contains jpg files and dir can list all jpg files shown above the screen shot.
if s = dir; it works well, but s = dir('*.jpg'); it does not work
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)
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

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 9 Mai 2018
Modifié(e) : Jan le 9 Mai 2018
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 commentaires

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 le 10 Mai 2018
Modifié(e) : li yan le 10 Mai 2018
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.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 9 Mai 2018
images = dir('*.jpg') ;
iwant = {images(:).name}' ;

1 commentaire

it does not work correctly.
images = dir('*.jpg') ;
this just return a struct with one element, like this:

Connectez-vous pour commenter.

Catégories

En savoir plus sur Share and Distribute Software dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by