How do I pause Matlab execution to wait for file edits

11 ビュー (過去 30 日間)
Anthony
Anthony 2019 年 4 月 18 日
回答済み: Jan 2019 年 4 月 19 日
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.

回答 (1 件)

Jan
Jan 2019 年 4 月 19 日
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.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by