GUI - 'For' loop not working correctly

4 views (last 30 days)
Ellis Berry
Ellis Berry on 9 Jun 2016
Edited: Stephen23 on 9 Jun 2016
Hi everyone,
I have a very trivial issue here but I cannot see what I have done wrong. In a for loop I have created I get the code to retrieve the images from a folder (myFolder) and then run some code on them. It processes one picture at a time and works well, but for some reason doesn't process them in order. In the folder, the images are numbered from '1' to '33' so it should process 1 first and 33 last. However, it processes them randomly, number 9 being the last one to get processed for some bizarre reason! Here is my code, can anyone please see how I have misused the for loop for this to happen? :
% --- Executes on button press in pushbutton4. RUN PROGRAMME
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%The picture interval is entered in editbox1, if not, this error message
%appears.
Interval=str2num(char(get(handles.edit1,'String'))); %Prompt for user to enter camera interval.
if isempty(Interval)
errordlg('Error, please load pictures to be processed and enter a picture time interval before clicking Run.');
else
outDir = handles.outDir;
inDir = handles.inDir;
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.inDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(inDir, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
h=waitbar(0, 'Please Wait...');
for k=1:length(theFiles)
RGB = imread(fullfile(inDir, theFiles(k).name));
perc = numel(theFiles);
newRGB = imcrop(RGB,[handles.xMin handles.yMin handles.xMax-handles.xMin handles.yMax-handles.yMin]);
% Convert RGB image to chosen color space
I = rgb2hsv(newRGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.053;
channel1Max = 0.083;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.116;
channel2Max = 0.130;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.608;
channel3Max = 0.643;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Invert mask
BW = ~BW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
%newName = sprintf('Image_%d.jpg', k);
newName = ['Processed Image', num2str(k), '.jpg'];
newFile = fullfile(outDir, newName);
imwrite(BW, newFile);
%Update waitbar with current image/total number of images to process:
waitbar(k/perc, h);
drawnow;
end
delete(h);
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.outDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
thisString = fprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
OutputFileNames{k} = theFiles(k).name;
set(handles.listbox2, 'String', OutputFileNames); %listbox2 will display file names of processed images to listbox2.
drawnow; % Force immediate screen repaint.
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
time=((k-1)*Interval);
if black>(numel(image)*0.0193); %if black pixels is more than 1.93% of pixels in image, experiment complete. Value can be altered.
disp('The experiment is complete at this stage!')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
disp('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
Status = ('Complete');
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
Status = ('Incomplete');
end
PhotoDetails={fullFileName, black, time, Status};
Matrix(k,:)=PhotoDetails; %Matrix of three variables produced.
guidata(hObject,handles);
end
end
Header={'Image', 'Number of Black Pixels', 'Time (Seconds)', 'Status'};
xlswrite('PhotoResults', Header, 'Results');
xlRange='A2';
xlswrite('PhotoResults', Matrix, 'Results', xlRange);
Now, here is what the output of running this code, as you can see it processes the images in a random order, not from 1 to 33!! :
Now reading H:\Documents\o2\Processed Image1.jpg0 The experiment is not complete yet. The number of Black Pixels is: 27
Now reading H:\Documents\o2\Processed Image10.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image11.jpg1 The experiment is not complete yet. The number of Black Pixels is: 12
Now reading H:\Documents\o2\Processed Image12.jpg1 The experiment is not complete yet. The number of Black Pixels is: 13
Now reading H:\Documents\o2\Processed Image13.jpg1 The experiment is not complete yet. The number of Black Pixels is: 24
Now reading H:\Documents\o2\Processed Image14.jpg1 The experiment is not complete yet. The number of Black Pixels is: 117
Now reading H:\Documents\o2\Processed Image15.jpg1 The experiment is not complete yet. The number of Black Pixels is: 18
Now reading H:\Documents\o2\Processed Image16.jpg1 The experiment is not complete yet. The number of Black Pixels is: 10
Now reading H:\Documents\o2\Processed Image17.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image18.jpg1 The experiment is not complete yet. The number of Black Pixels is: 159
Now reading H:\Documents\o2\Processed Image19.jpg1 The experiment is not complete yet. The number of Black Pixels is: 41
Now reading H:\Documents\o2\Processed Image2.jpg0 The experiment is not complete yet. The number of Black Pixels is: 0
Now reading H:\Documents\o2\Processed Image20.jpg1 The experiment is not complete yet. The number of Black Pixels is: 229
Now reading H:\Documents\o2\Processed Image21.jpg1 The experiment is not complete yet. The number of Black Pixels is: 194
Now reading H:\Documents\o2\Processed Image22.jpg1 The experiment is not complete yet. The number of Black Pixels is: 110
Now reading H:\Documents\o2\Processed Image23.jpg1 The experiment is not complete yet. The number of Black Pixels is: 225
Now reading H:\Documents\o2\Processed Image24.jpg1 The experiment is not complete yet. The number of Black Pixels is: 375
Now reading H:\Documents\o2\Processed Image25.jpg1 The experiment is not complete yet. The number of Black Pixels is: 125
Now reading H:\Documents\o2\Processed Image26.jpg1 The experiment is not complete yet. The number of Black Pixels is: 869
Now reading H:\Documents\o2\Processed Image27.jpg1 The experiment is not complete yet. The number of Black Pixels is: 568
Now reading H:\Documents\o2\Processed Image28.jpg1 The experiment is not complete yet. The number of Black Pixels is: 547
Now reading H:\Documents\o2\Processed Image29.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1121
Now reading H:\Documents\o2\Processed Image3.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image30.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1875
Now reading H:\Documents\o2\Processed Image31.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1617
Now reading H:\Documents\o2\Processed Image32.jpg1 The experiment is not complete yet. The number of Black Pixels is: 2108
Now reading H:\Documents\o2\Processed Image33.jpg1 The experiment is not complete yet. The number of Black Pixels is: 1849
Now reading H:\Documents\o2\Processed Image4.jpg0 The experiment is not complete yet. The number of Black Pixels is: 21
Now reading H:\Documents\o2\Processed Image5.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Now reading H:\Documents\o2\Processed Image6.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image7.jpg0 The experiment is not complete yet. The number of Black Pixels is: 2
Now reading H:\Documents\o2\Processed Image8.jpg0 The experiment is not complete yet. The number of Black Pixels is: 1
Now reading H:\Documents\o2\Processed Image9.jpg0 The experiment is not complete yet. The number of Black Pixels is: 5
Does anyone have any idea what I have done wrong?
Thanks so much,
Ellis
  1 Comment
Stephen23
Stephen23 on 9 Jun 2016
Edited: Stephen23 on 9 Jun 2016
@ Ellis Berry: the order is certainly not random, it is actually ASCIIbetical order. You just need to use my FEX submission to get them into the order that you want:

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 9 Jun 2016
The files are being processed in alphabetical order. '10' is alphabetically before '2.'
Note: the order in which dir() returns the files is up to the operating system, and in turn operating systems typically leave that up to the file system (e.g., could be different for NTFS vs FAT, or could be different for NTFS configured in different ways.) You should never assume that dir() has returned the contents in any particular order. (For future reference: you should not assume that '.' and '..' are the first two entries in a general dir(), as there are cases where they are not the first two entries!)

Adam
Adam on 9 Jun 2016
Edited: Adam on 9 Jun 2016
Strings order alpha-numerically so anything starting with 9 will come after anything starting with 1, irrespective of whether it is 1 or 10 or 1234.
Not sure if that is your full problem, but it is just a quick observation and seems to match what you show - the order certainly isn't random.

Community Treasure Hunt

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

Start Hunting!