MATLAB event and infinite sleeping or checking loo

2019-02-11 04:51发布

I need to perform data analysis on files in a directory as they come in.

I'd like to know, if it is better,

  1. to implement an event listener on the directory, and start the analysis process when activated. Then having the program go into sleep forever: while(true), sleep(1e10), end

  2. or to have a loop polling for changes and reacting.

I personally prefer the listeners way, as one is able to start the analysis twice on two new files coming in NEARLY the same time but resulting in two events. While the other solution might just handle the first one and after that finds the second new data.
Additional idea for option 1: Hiding the matlab GUI by calling frames=java.awt.Frame.getFrames and setting frames(index).setVisible(0) on the index matching the com.mathworks.mde.desk.MLMainFrame-frame. (This idea is taken from Yair Altman)

Are there other ways to realize such things?

1条回答
贪生不怕死
2楼-- · 2019-02-11 05:26

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

查看更多
登录 后发表回答