Is it really the case that you want to get a directory listing of files whose names start with "Dataset 1" followed by any number of characters with a .tif extension? Such as Dataset 1July-20-1969_eagle.tif ? Or are you wanting to look inside a folder named Dataset 1 for all .tif files?
I really recommend you get into the habit of using fullfile()
Use the debugger to find the cause of the problem. Type e.g. this in the command window:
dbstop if error
Run the code again. When Matlab stops at the error, check the currently used variables:
size(img)
size(images)
Maybe the rgb2gray is important?
Some hints:
fullfile(a,b) is smarter than [a, '/', b], because it considers the fileseparators of the platform.
The names of the variables look confusing: "filenames" is not a list of file names, "a" is meaningless,
labels = reshape(labels, [length(labels), 1]) can be written as labels = labels(:).
The condition if subDir == '.' | strcmp(subDir, '..') == 1 can be simplified. Remember that == is the elementwise operation and subDir is a char vector. If subDir is '..', the comparison replied [true, true]. Because if requires a scalar condition, Matlab inserts an all implicitly. Then this is suffcient actually: if subDir == '.', because it catchs '..' also. Writing this explicitly is nicer to read: if all(subDir == '.'). This would trigger also for '...', but this is not an allowed file name under Windows, but maybe under Linux (?). Therefore I prefer the explicit: if strcmp(subDir, '.') || strcmp(subDir, '..')
Prefer to work with cell strings (or strings) instead of CHAR matrices. Replace:
imageFiles = '';
...
files = dir(strcat(dirPath, '/', subDir));
fileFlags = ~[files.isdir];
files = files(fileFlags);
for j = 1: length(files)
filename = strcat(subDir, '/', files(j).name);
imageFiles = strvcat(imageFiles, filename);
end
by
imageFiles = {};
...
files = dir(fullfile(dirPath, subDir));
fileNames = {files(~[files.isdir]).name};
imageFiles = cat(2, imageFiles, fileNames);
...
% Instead of imageFiles(j, :) use imageFiles{j} afterwards
Best Answer