Trouble calling files using dir command

38 ビュー (過去 30 日間)
Charlie Schmidt
Charlie Schmidt 2018 年 11 月 7 日
回答済み: Nick 2018 年 11 月 7 日
ImageFiles = dir(DBImages);
%%%extracts information of all files whose filename end with `.png?.
^^ This line of code was given to me by my professor and seems to have worked for everyone else, but I can't get it to call the images in my DBImages folder. When I try to run the script, I get this error:
Undefined function or variable 'DBImages'.
I'm not really sure why my DBImages file can't be found; I selected it and clicked on "add folder and subfolders to path," which I figured would make it accessible, but I'm still receiving the asme error. There are no spelling mistake here either. I'm not sure, but I don't think I should need any special toolboxes or anything, as DBImages is just a folder with a few .m files and a lot of .png files. Any thoughts?
  1 件のコメント
Stephen23 2018 年 11 月 7 日
"I'm not really sure why my DBImages file can't be found"
The input to dir is not a file, it is a string/character vector. The input specifies the location/s where dir should search for files/folders. dir then returns the names (and some other properties) of those files/folders.
The error message you show has nothing to do with dir: it is simply because you have not defined what DBImages is, before you try to use it. It is like asking MATLAB to calculate a sine value:
but you do not tell MATLAB what x is. What do you expect MATLAB to do?



jonas 2018 年 11 月 7 日
編集済み: jonas 2018 年 11 月 7 日
I'm not surprised it does not work. The variable DBImages should probably be assigned a string at some point prior to calling dir. Just learn how the function dir works instead:
files = dir('folderpath\*.png')
For example
files = dir('C:\MyImages\*.png')
if DBImages is the folder name, then you probably just failed to make it a string
files = dir('DBImages')
Although the above is possible, I recommend adding the entire folder path, with the only downside being that you have to alter the script if you move the folder.

その他の回答 (1 件)

Nick 2018 年 11 月 7 日
The dir() function expects a character array as input and a struct containing information of the files in that directory (or an empty struct if it is not a correct file path). Your error comes from the fact that DBImages is not declared yet when you call the function and without quotation marks MATLAB thinks you are referring to a variable or a function when using text.
So the short answer would be you need to use quotation marks:
ImageFiles = dir('DBImages');
This however assumes you are in the parent directory of a folder called DBImages. And the structure will contain all possible files inside this folder.
Another approach is using a full file path and adding the extension like this
ImageFiles = dir(fullfile('YourFilePath','*.png'));
With YourFilePath being the full file path to the directory containing the PNG images. The '*.png' will filter out everything that does not end with .png. The output struct will contain folder, name, ...etc. for each file. You can then use use it like this:
% short example how to use the struct - prints file names in that
% folder to command window
for ii = 1:numel(ImageFiles)




Community Treasure Hunt

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

Start Hunting!

Translated by