Using AppDesigner with FileSystemWatcher
Show older comments
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
Geoff Hayes
on 29 Jun 2020
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?
Darrell
on 29 Jun 2020
Darrell
on 29 Jun 2020
Geoff Hayes
on 29 Jun 2020
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. (?)
Darrell
on 29 Jun 2020
Geoff Hayes
on 30 Jun 2020
Glad it worked out! :)
Accepted Answer
More Answers (0)
Categories
Find more on Desktop in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!