MATLAB event and infinite sleeping or checking loop

后端 未结 1 523
遇见更好的自我
遇见更好的自我 2021-01-03 13:55

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 d

相关标签:
1条回答
  • 2021-01-03 14:01

    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

    0 讨论(0)
提交回复
热议问题