How do I pause Matlab execution to wait for file edits

11 visualizaciones (últimos 30 días)
Anthony
Anthony el 18 de Abr. de 2019
Respondida: Jan el 19 de Abr. de 2019
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.

Respuestas (1)

Jan
Jan el 19 de Abr. de 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.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by