How do I pause Matlab execution to wait for file edits

11 views (last 30 days)
Hello, I am trying to open and edit a file within a Matlab function and pause Matlab execution until the file is closed. However, the code I have implemented takes a very long time to return when the file being editied is closed by the user. My code is listed below:
function [x] = DoSomething(u)
ef = matlab.desktop.editor.openDocument(fullfile(pwd,'Inputs.m')); % opens the file in the editor
waitfor(ef,'Opened',0); % this line waits for the editor property 'Opened' to change from a 1 to a 0 when the file is closed
x = u;
return
This coding works within the function however the waitfor takes a very long time to detect that the file was closed in the editor and then allow Matlab to continue computations within the m-file. The time to detect closing can vary up to several minutes.

Answers (1)

Jan
Jan on 19 Apr 2019
It is a bad idea to edit M-files during the execution. It would be much better to edit a file, which contains the modified parameters only. Using a standard GUI to modify the parameters is smarter also, than using Matlab's editor as GUI.
You could use a listener to check for changed files:
w = System.IO.FileSystemWatcher(pwd);
w.Filter = 'Input.m';
w.NotifyFilter = System.IO.NotifyFilters.LastWrite;
addlistener(w, 'Changed', @changedFcn);
w.EnableRaisingEvents = true;
Now you have to implement the resuming in the changedFcn. But I think this is too indirect. Use a standard GUI instead to modify the parameters and leave the M-files untouched.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!