Filter löschen
Filter löschen

why I cannot list specify files with function dir

5 Ansichten (letzte 30 Tage)
li yan
li yan am 9 Mai 2018
Bearbeitet: li yan am 10 Mai 2018
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 Kommentare
Guillaume
Guillaume am 9 Mai 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
li yan am 9 Mai 2018
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 9 Mai 2018
Bearbeitet: Jan am 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 Kommentare
Guillaume
Guillaume am 9 Mai 2018
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 am 10 Mai 2018
Bearbeitet: li yan am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 9 Mai 2018
images = dir('*.jpg') ;
iwant = {images(:).name}' ;
  1 Kommentar
li yan
li yan am 9 Mai 2018
it does not work correctly.
images = dir('*.jpg') ;
this just return a struct with one element, like this:

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by