【问题标题】:Poll Windows folder for new files in Matlab? [duplicate]在 Matlab 中轮询 Windows 文件夹中的新文件? [复制]
【发布时间】:2014-03-21 14:01:40
【问题描述】:

我需要编写一个 MATLAB 脚本来轮询 Windows 7 中的文件夹,以便在将新图像文件写入磁盘时,它会自动显示在 MATLAB 中。我需要帮助编写投票部分。

根据一些谷歌搜索,现在我有:

dir_content = dir(fbdir);
filenames = {dir_content.name};
current_files = filenames;

while true;
  dir_content = dir(fbdir);
  filenames = {dir_content.name};
  new_files = setdiff(filenames,current_files);
  if ~isempty(new_files)
    DO MY THING HERE
  end;           
end   

但问题是它不能捕获所有图像,有时图像可能会显示但未完全写入磁盘,当我尝试加载时在 Matlab 中引发错误。是否有更好的方法来轮询新文件(仅那些已完全写入磁盘的文件)?

【问题讨论】:

  • 您是在寻找新的图像文件还是任何新文件?
  • 特别是新的 .PNG 或 .JPG 文件。如果有帮助,我还可以预测新文件的名称。但是我需要在它写入磁盘后立即加载它(使用 Matlab 的单独工具箱,但我认为这与这里无关)。
  • 谢谢,看来使用计时器对象解决了。
  • 当你找到一个新文件时,你应该把你的load() 放在一个带有pause(0.2) 左右的重试循环中,以解决你在尝试加载部分编写的文件时遇到的错误文件。链接示例中的代码不处理这种情况。您可以等到它的大小停止增长,但这只是一种启发式方法,无论如何您都必须尝试/捕获负载才能真正避免错误。

标签: windows matlab polling


【解决方案1】:

试试这个 -

initial_count = numel(dir('*.jpg')) + numel(dir('*.png'));
while 1
    count = numel(dir('*.jpg')) + numel(dir('*.png'));
    if count>initial_count
        DO MY THING HERE %%// New image file found
        initial_count  = count;
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2014-12-03
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多