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

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

问题描述

我需要对目录中的文件进行数据分析.

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. 在目录上实现一个事件监听器,并在激活时启动分析过程.然后让程序永远进入睡眠状态:while(true), sleep(1e10), end

或循环轮询更改并做出反应.

or to have a loop polling for changes and reacting.

我个人更喜欢侦听器的方式,因为可以对几乎同时出现的两个新文件进行两次分析,但会导致两个事件.而另一种解决方案可能只处理第一个,然后找到第二个新数据.
选项 1 的附加想法:通过调用 frames=java.awt.Frame.getFrames 并在 上设置 frames(index).setVisible(0) 隐藏 matlab GUI>index 匹配 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 的强大功能.p>

In this case, (if you are using Windows), the best way is to use the power of .NET.

fileObj = System.IO.FileSystemWatcher('c:work	emp');
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 );

eventhandlerChanged 是你的回调函数.

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天全站免登陆