MATLAB事件和无限睡眠或检查循环 [英] MATLAB event and infinite sleeping or checking loop

查看:302
本文介绍了MATLAB事件和无限睡眠或检查循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对目录中的文件执行数据分析。

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

我想知道,如果更好,


  1. 在目录上实现事件侦听器,并在激活时启动分析过程。然后让程序永远进入睡眠状态: while(true),sleep(1e10),end

或者有一个循环轮询来进行更改和反应。

or to have a loop polling for changes and reacting.

我个人更喜欢听众的方式,对两个新的文件进行两次分析,最近在同一时间进行,但是导致两个事件。而另一个解决方案可能只是处理第一个,之后找到第二个新数据。

选项1的附加思想:通过调用 frames = java.awt隐藏matlab GUI。 Frame.getFrames 并在索引上设置 frames(index).setVisible(0)匹配 com.mathworks.mde.desk.MLMainFrame -frame。 (这个想法来自于 Yair Altman

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?

推荐答案

在这种情况下,(如果您使用的是Windows),最好的方法是使用 .NET 的权力。

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);

有不同的事件类型,您可以为他们使用相同的回调或不同的事件类型: p>

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 );

其中 eventhandlerChanged 是您的回调函数。 / p>

Where eventhandlerChanged is your callback function.

function eventhandlerChanged(source,arg)
   disp('TXT file changed')
end

没有必要使用 sleep 或轮询。如果你的程序是基于UI的,那么没有别的办法,当用户关闭数字时,程序已经结束了。事件回调与按钮点击完全相同。如果你的程序是类似脚本的,你可以使用一个无限循环。

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.

更多信息在这里: http://www.mathworks.com/help/matlab/matlab_external/working-with-net-events- in-matlab.html

这篇关于MATLAB事件和无限睡眠或检查循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆