Trouble calling files using dir command

조회 수: 45 (최근 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
Stephen23 2018년 11월 7일
편집: 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:
sin(x)
but you do not tell MATLAB what x is. What do you expect MATLAB to do?

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
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
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)
fprintf([ImageFiles(ii).name,'\n'])
end

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by