errors with polling directory for new files to process?

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

1 Comment

What is the actual problem? After compiling the file import does not work: "Matlab:imagesci:imread:fileFormat" (is this really the complete message?). Or do you ask about the polling method?

Sign in to comment.

 Accepted Answer

Jan
Jan on 9 Aug 2017
Edited: Jan on 9 Aug 2017
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

3 Comments

Jan - thanks for the advice. I will give it a try and report back.
Hello,
Code works but I cannot define more than one notify filter. Could you show how?
Best wishes,
w.NotifyFilter = w.NotifyFilter Or System.IO.NotifyFilters.FileName
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct
matrices, use brackets instead of parentheses.
You need to use the bitor() function instead of what Jan has there:
w.NotifyFilter = bitor(w.NotifyFilter, System.IO.NotifyFilters.Attributes)

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 8 Aug 2017

Commented:

on 10 Aug 2021

Community Treasure Hunt

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

Start Hunting!