I need to perform data analysis on files in a directory as they come in.
I\'d like to know, if it is better,
to implement an event listener on the d
In this case, (if you are using Windows), the best way is to use the power of .NET
.
fileObj = System.IO.FileSystemWatcher('c:\work\temp');
fileObj.Filter = '*.txt';
fileObj.EnableRaisingEvents = true;
addlistener(fileObj,'Changed',@eventhandlerChanged);
There are different event types, you can use the same callback for them, or different ones:
addlistener(fileObj, 'Changed', @eventhandlerChanged );
addlistener(fileObj, 'Deleted', @eventhandlerChanged );
addlistener(fileObj, 'Created', @eventhandlerChanged );
addlistener(fileObj, 'Renamed', @eventhandlerChanged );
Where eventhandlerChanged
is your callback function.
function eventhandlerChanged(source,arg)
disp('TXT file changed')
end
There is no need to use sleep
or polling. If your program is UI based, then there is nothing else to do, when the user closes the figure, the program has ended. The event callbacks are executed exactly like button clicks. If your program is script-like, you can use an infinite loop.
More info in here: http://www.mathworks.com/help/matlab/matlab_external/working-with-net-events-in-matlab.html