问题描述
我试图创建一个函数做一件事:返回最近的文件名,实时
I'm trying to create a function to do one thing: return the most recent file name, in real time.
现在我发现这佳作通过谷歌搜索:
Now I found this fine piece by googling:
function FileWatch(pathToWatch)
txtDetectedFiles = createFigure;
fileObj = System.IO.FileSystemWatcher(pathToWatch);
fileObj.EnableRaisingEvents = true;
changeListener =addlistener(fileObj, 'Changed', @onChange); %need to keep in scope
function txtDetectedFiles = createFigure % Creates Figure
figHdl = figure('Name','FileWatcher',...
'Menubar','none',...
'Toolbar','none',...
'NumberTitle','off',...
'Units','normalized',...
'Position',[0.4,0.4,0.4,0.2]);
uicontrol('Parent',figHdl,...
'Style','text',...
'Units','normalized',...
'Position',[0.1,0.9,0.8,0.08],...
'String',['Path Watched: ',pathToWatch]);
txtDetectedFiles = uicontrol('Parent',figHdl,...
'Style','edit',...
'Enable','inactive',...
'Units','normalized',...
'Position',[0.1,0.05,0.8,0.45],...
'Max',3);
end %createFigure
function onChange(~,evt)
existStr = get(txtDetectedFiles,'String');
if isempty(existStr)
existStr = {};
end %if
existStr{length(existStr)+1} = ['New file Detected: ',char(evt.FullPath.ToString())];
set(txtDetectedFiles,'String',existStr);
newestfile = (char(evt.FullPath.ToString()));
assignin ('base','newestfile',newestfile); % Sets Variable to the newestfile
end %onChange
end %FileWatch
据响应远远比我的previous方法更好,它的工作原理,我加了 newestfile
和 assignin
来最新的文件返回一个字符串。
It responds far better than my previous methods and it works,I added newestfile
and assignin
to return the most recent file as a string.
现在的问题是但是,我似乎无法缩短到这一更基本的功能,无需数字。我只是需要这个看目录,并迅速返回新文件。当我删除的数字( createfigure
),它没有响应,所以它似乎是一个要求......
The problem is however, I cannot seem to shorten this into more basic function without the figures. I simply need this to watch a directory, and return new files quickly.When I delete the figures (createfigure
), it does not respond, so it seems a requirement...
是否有人可以帮助我呢?所以一旦多了,我在寻找这方面的一个简单的,非GUI /数字/ uicontrol版本。
Can someone please help me with this? So once more, I'm looking for a simpler, non GUI/figure/uicontrol version of this.
推荐答案
问题是,如code评论说,你必须保持的ChangeListener
活着。也就是,保持一个参考的地方在那里它不被破坏。在与-数字版本的参考大概保持在嵌套函数的工作空间(另一个理由不使用它们)。
The issue is that, as the comments in the code say, you have to keep changeListener
alive.That is, keep a reference somewhere where it is not destroyed.In the with-figures version a reference is probably kept in the workspace of the nested functions (yet another reason not to use them).
对我来说,以下工作:
function listeners = FileWatch(pathToWatch, callback)
persistent Listeners;
% to check/clear the existing ones:
listeners = [];
switch pathToWatch
case 'clear'
Listeners = [];
return
case 'list'
listeners = Listeners;
return;
end
fileObj = System.IO.FileSystemWatcher(pathToWatch);
fileObj.EnableRaisingEvents = true;
changeListener = addlistener(fileObj, 'Changed', @onChange); %need to keep in scope
% alternatively:
% taking the callback as input-argument would make it a more universal function:
% changeListener = addlistener(fileObj, 'Changed', callback);
if isempty(Listeners)
Listeners = changeListener;
else
Listeners(end+1) = changeListener;
end
end
function onChange(~,evt)
newestfile = char(evt.FullPath.ToString());
fprintf('File-Event: %s\n', newestfile);
% do whatever you want to do with it :)
end %onChange
这篇关于MATLAB System.IO.FileWatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!