MATLAB: Is there any reason why this loop is not reading the images in order? I noticed that once it starts running, it reads the images in a random order. It prints the image being read on each itereation, and i dont know why it does not start at image 1.

csvimage processingloop

myDir = 'projectImages';
% Gets list of all files with the corresponding file extension
f = fullfile(myDir, '*.jpg');
theFiles = dir(f);
vector_img = zeros(28^2, length(theFiles ));
for k = 1 : length(theFiles)
baseFile = theFiles(k).name;
completeFile = fullfile(myDir, baseFile);
fprintf(1, 'Now reading %s\n', completeFile); %prints the current image being processed.
img = imread(completeFile); %reads image
vector_img(:,k) = img_28(:); %vectorizes the image


img_gray = rgb2gray(img); %turns image to grayscale
vector_img(:,k+1) = img_28(:); %vectorizes the image
img_28 = imresize(img_gray, [28 28]); %resizes image
vector_img(:,k+2) = img_28(:); %vectorizes the image
end
csvwrite('completeimgdata.csv', vector_img'); %writes the vectorized images to csv file

Best Answer

dir() does not sort the returned information . It uses the order of entries returned by the operating system . In turn the operating system delegates to filesystem code. Different filesystemss have different sorting rules .
Some filesystems are defined as sorting directories as most recently used first out of the complete directory . Other filesystems divide the directory into blocks and rewrite one block at a time according to most recently used within the block . Others use binary tree representations .
MS Windows NTFS does not appear to formally define the sorting order . However in practice it appears to sort according to the utf16 non-canonical Unicode representation of the filenames . Apple's HFS+ is defined to sort according to utf16 representation of canonical Unicode .
Now one aspect of sorting according to complete filename including extension is that period sorts before the digits. That means that AA1.csv sorts before AA10.csv but it also means that AA10.csv sorts before AA2.csv . Files do not sort in the order 1 2 3 4 5 6 7 8 9 10 11 12 and so on: they sort in the order 1 10 11 12 ... 19 2 21 ... 29 3 30 31 ... 39.
That is probably what you are observing : they probably are in sorted order but the sorting is by characters not dictionary order . Likewise Spanish LL will not sort immediately after L as is defined for Spanish dictionary order .
You might want to look in the file exchange for natsort routines .