how to check a path? file or folder?

13 vues (au cours des 30 derniers jours)
Mr M.
Mr M. le 12 Fév 2015
Modifié(e) : Stephen23 le 22 Fév 2015
I would like a path input argument, which can be path of a jpg file or a folder. If it is a folder, I would like to go through its content and use all jpg images in there. How to check the path? And how to go through?
  2 commentaires
Mr M.
Mr M. le 12 Fév 2015
Is it possible to select files from a GUI?
Stephen23
Stephen23 le 12 Fév 2015
Modifié(e) : Stephen23 le 12 Fév 2015

Connectez-vous pour commenter.

Réponses (3)

Stephen23
Stephen23 le 12 Fév 2015
Modifié(e) : Stephen23 le 22 Fév 2015
You can check for the existence of a specific file or a folder using exist . You can use the second argument to tell the function what kind of things you want to check for, and check that the output is the expected number:
2==exist('root\directory\filename.jpg','file')
To locate all .JPG files in a specific directory, use dir :
S = dir('root\directory\*.jpg')
You can use relative or absolute paths, and the function returns a structure containing information that you might find useful, particularly the isdir logical.

Michael Haderlein
Michael Haderlein le 12 Fév 2015
Use the exist() function. It will return 2 for a file, 7 for a folder, and, in case the path does not exist at all, you'll get the value 0 and can do error handling.
  1 commentaire
Michael Haderlein
Michael Haderlein le 12 Fév 2015
...or, as Stephen has mentioned, use dir(). You'll need that to go through, anyway.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 18 Fév 2015
You say "If it is a folder, I would like to go through its content and use all jpg images in there." So see my demo which goes through the folder and all its subfolders getting image filenames. You can then display or process them - whatever you want.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by