MATLAB event and infinite sleeping or checking loop

百般思念 提交于 2019-11-29 23:32:46

问题


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:


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



来源:https://stackoverflow.com/questions/10915675/matlab-event-and-infinite-sleeping-or-checking-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!