How to modify the code of a running script?

45 views (last 30 days)
Pablo
Pablo on 26 Sep 2013
Answered: Walter Roberson on 26 Sep 2013
Hi all.
I have been running a machine learning script for several days now, but it converges extremely slowly. Since I will not have time to modify the code and start the process over again, I was wondering whether it is possible to modify the code of a running script in Matlab; for instance, I'd like to add an additional condition to the outer loop (a while loop) to make it stop after a given number of iterations. That change would be simple and easy in my case because I am keeping track of the iteration number.
Another helpful option would be to use a command to retrieve a copy of the data currently being used by a running script.
Is any of these two possibilities feasible in Matlab?

Answers (2)

Walter Roberson
Walter Roberson on 26 Sep 2013
You cannot modify a running function if you are not in the debugger (if you are, there are provisions for edit-and-test if you are in "cell mode").
You can (externally) modify the .m code for a function while the function is running. The change will not be noticed until at least a "clear" is done against the function name, or a "clear all" is done, or a "rehash". Even then, if the function is actively being used or there is a function handle to it, then the active instance itself will not be updated, and the function handles will not be updated. The change affects only future executions of the function by name or the future creation of function handles to the function.
For example,
function AFUN
for K = 1 : 100000
BFUN
end
end
function BFUN
do something
end
If you were to modify BFUN while an instance of BFUN is executing, the instance would finish executing using the old code. Then, when AFUN went to call upon BFUN by name the next time, it would still not notice that that BFUN had changed, not until somehow a "clear BFUN" or "clear all" or "rehash" were to be executed.
function AFUN
for K = 1 : 10000
fid = fopen('BFUN.m','w');
fprintf(fid, 'function BFUN\ndisp(%d)\nend\n', K);
fclose(fid);
clear BFUN
BFUN
end
end
In the above, BFUN.m is modified, and the the "clear BFUN" tells MATLAB to let go of the old version. Then BFUN is executed by name and the new version is picked up.

dpb
dpb on 26 Sep 2013
Nope...the code is loaded in memory so changing the m-file doesn't do anything to the running process.
And, unless you wrote the script to have occasional data dumps for restart purposes, etc., there's no accessing it from another process, either, sorry.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!