Using AppDesigner with FileSystemWatcher

I am trying to create a MATLAB app that uses FileSystemWatcher to call an event handler each time a specific file has changed--using version 2018b. I have created the following two functions in Code View using the appdesigner; however the app never calls 'eventhandlerChanged' function. I've spent a lot of time trying to get this to work, so any help would be greatly appreciated.
% Button pushed function: TestFSWButton
function AddFSW(app, event)
fileObj = System.IO.FileSystemWatcher('D:\Matlab\Work');
fileObj.Filter = 'test.csv';
fileObj.EnableRaisingEvents = true;
addlistener(fileObj,'Changed',@app.eventhandlerChanged);
end
function eventhandlerChanged(app,evt)
tic;
toc;
end

6 Comments

Darrell - have you tried removing the filter or just using '*.csv'? Have you tried using code similar to the above but not from within App Designer?
I tried using “*.csv”, but that didn’t work either. I also tried numerous variations from the internet, but none of those worked either. I haven’t tried it outside the Matlab App designer, because I need it to work within my Matlab app interface. I’ve been able to successfully use a timer function, but that’s not really what I want. I am now starting to wonder if the Matlab designer has an issue working with the MS file system watcher function.
I just tested the code outside the App Designer GUI and the eventhandler worked correctly. Therefore, I believe there is something within the App Designer interface that is preventing the filesystemwatcher from calling the eventhandler.
Darrell - from Create event listener bound to event source it shows that the call to addlistener returns an object. I wonder if you need to make that object a member of your app? (i.e. assign the result of this call to a member within your App class.) Because if you don't do this, then the local object - created within the AddFSW method - would be destroyed as the method completes and you would no longer have an object listening for changes to your file. (?)
Geoff,
That worked! Below is the final code.
properties (Access = private)
myFSW % FileSystemWatcher object
end
function eventhandlerChanged(app,~,~)
% do stuff
disp('Successful!');
end
% Button pushed function: TestButton
function AddFSW(app, event)
app.myFSW = System.IO.FileSystemWatcher('D:\Matlab\Work');
app.myFSW.Filter = 'test.csv';
app.myFSW.EnableRaisingEvents = true;
addlistener(app.myFSW,'Changed',@app.eventhandlerChanged);
end
Note. If you modify the test file using Notepad the method will fire twice; but if you are watching for an actual file it all works correctly. Thank you!
Glad it worked out! :)

Sign in to comment.

 Accepted Answer

Summary of issue: from Create event listener bound to event source it shows that the call to addlistener returns an object. In the above code, this object is local to the AddFSW method and so would be destroyed when the method "completes" which would mean that we no longer have an object listening for changes to the file. The fix is to make the listener object a member of the app so that it exists for the lifetime of the app (or until removed or deleted if needed).

More Answers (0)

Categories

Products

Asked:

on 29 Jun 2020

Answered:

on 30 Jun 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!