在每个实例由单独的程序创建文件后,使用MATLAB实时处理文件 [英] Using MATLAB to process files in real-time after every instance a file is created by a separate program
问题描述
我正在使用MATLAB处理由相机创建并存储在Windows目录中的图像文件.我正在尝试将反馈合并到相机的控制中,因此,每次创建新图像并出现在目录中时,都要求MATLAB处理图像.我从未创建过可连续运行并等待事件发生的MATLAB程序.
I am using MATLAB to process image files that are created by a camera and stored in a directory on Windows. I am trying to incorporate feedback into control of the camera and therefore require MATLAB to process an image every time a new image is created and appears in the directory. I have never created a MATLAB program that runs continuously and waits for an event to occur.
从我在网上阅读的内容来看,最好的选择似乎是使用计时器对象,并让MATLAB程序重复读取目录的内容.这是一个好的方法还是我可以实现的替代方法?
我想知道是否有一种方法可以通过目录中文件的出现来触发" MATLAB程序,而不是不断调查该目录的内容.我希望是因为目录填满后,我发现MATLAB中的"dir"命令确实很慢;速度太慢,可能无法以所需的速度处理图像.
I'm wondering if there is a way the MATLAB program can be "triggered" by the appearance of a file in a directory as opposed to constantly surveying the contents of this directory. I hope there is because as the directory fills up I find that the "dir" command in MATLAB is really slow; slow enough that I may not be able to process images as fast as I require.
作为后续.关于如何部署此程序,有什么建议吗?我喜欢的一个想法是带有开始"和停止"按钮的简单GUI.
As a follow up. Are there any recommendations about how to deploy this program? An idea I like is a simple GUI with a "start" and "stop" button.
谢谢.
推荐答案
您可以执行以下操作:
创建计时器对象,该对象将每10秒检查一次您的目录:
Create timer object, which will check your directory every 10 seconds:
t = timer('TimerFcn', @mycallback, 'Period', 10.0, 'ExecutionMode', 'fixedSpacing');
您的'mycallback'函数应如下所示:
DIR_TO_READ = 'C:\incoming-files';
DIR_TO_MOVE_PROCESSED = 'C:\processed-files';
% get list of files.
file_struct = dir(DIR_TO_READ)
% remove '.' and '..' directories
file_struct([file_struct.isdir]) = [];
for j = 1 : numel(file_struct)
current_file = file_struct(j).name;
full_filename = fullfile(DIR_TO_READ, current_file)
% add your processing of the file here
% e.g.
bla = imread(full_filename);
% now move the processed file to the processed file folder
movefile(full_filename, fullfile(DIR_TO_MOVE_PROCESSED, current_file))
end
现在您需要启动计时器对象
Now you need to start the timer object
start(t);
您可以使用以下方式停止计时器对象
You can stop the timer object with
stop(t);
这篇关于在每个实例由单独的程序创建文件后,使用MATLAB实时处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!