MATLAB: Errors with polling directory for new files to process

file processingMATLABwindows executable

I've returned to working on some code that I had put away for a while. the purpose is to actively poll a folder where images are saved, and process newly acquired/created image files. the actual processing of images works, so I've commented out that portion of the code, as I'm having trouble with the directory polling / working with multiple files, as well as the imread function in Windows.
I'm getting two errors now; I'll clarify the errors below.
1) when testing my code in Matlab, it seems to get caught in a loop. it will detect the presence of a new file, it will do some processing magic on it (basically a really simplified region analysis), and then start working on the other files in the same folder (which do not need to be analyzed).
2) after compiling to an executable, the executable version of the program gives an error with regards to "imread" saying that it's unable to determine the file format ("Matlab:imagesci:imread:fileFormat is the exact error). so it never actually gets to the image processing anyway.
for the record – only working with the basic Matlab license (the compiling of executable is being done by someone else with that license).
%display(['select the folder where front can images will be saved: ']);
t = timer('TimerFcn', @mycallback, 'Period', 10.0, 'ExecutionMode', 'fixedSpacing');
myFolder = uigetdir();
%textLabel = sprintf('select the folder to save images to', myfolder);
DIR_TO_READ = myFolder;
% get list of files.
file_struct = dir(DIR_TO_READ);
% remove '.' and '..' directories
file_struct([file_struct.isdir]) = [];
olddir = dir(myFolder);
while true
pause(1)
newdir = dir(myFolder);
if ~isequal(newdir, olddir)
fprintf('new files found\n');
olddir = newdir;
for j = 1 : numel(file_struct)
pause(5)
current_file = file_struct(j).name;
full_filename = fullfile(DIR_TO_READ, current_file);
while true
clc;
warning('off','images:initSize:adjustingMag');
loadimg = imread(full_filename);
%processing section
end
end
else
fprintf('no new files\n')
end
end

Best Answer

while true
clc;
warning('off','images:initSize:adjustingMag');
loadimg = imread(full_filename);
%processing section
end
This is an infinite loop, which processes the same image again and again. I assume that omitting the "while true" solves the problem directly.
If some warnigns would be shown, clc will hide them soon. Therefore I never use this function, because the output to the command window contains valuable debug information.
Note: Polling wastes some processing time, therefore an callback approach might be more usefull. But I'm not sure how this can be compiled and in works under Windows only:
w = System.IO.FileSystemWatcher(pwd);
% Watch a particular file type:
w.Filter = '*.jpg';
% Notify when the file changes
w.NotifyFilter = System.IO.NotifyFilters.LastWrite;
w.NotifyFilter = System.IO.NotifyFilters.DirectoryName
w.NotifyFilter = w.NotifyFilter Or System.IO.NotifyFilters.FileName
w.NotifyFilter = w.NotifyFilter Or System.IO.NotifyFilters.Attributes
w.IncludeSubdirectories = True
% Create a listener for change
addlistener(w, 'Changed', @changedFcn);
% Start watching.
w.EnableRaisingEvents = true;
function changedFcn(obj, eData)
fprintf('Changed: %s\n', char(eData.FullPath));
...
end