Stale files used instead of updated files
24 次查看（过去 30 天）
I frequently have the problem that I update a Matlab script (not necessarily a function), but when I run it, an old version is run instead. This old version doesn't seem to even need to exist on the hard drive, Matlab seems to be keeping a copy in memory. To work around it, I have to open the file and re-save it (unchanged). It then runs the updated version.
This makes it very difficult to quickly make and test changes. It's got to the point that I exit and restart Matlab when I want to be sure it won't run an old file version. Frankly, this behavior is driving me up a wall.
I only have this behaviour in Matlab, not in other programs. How can I stop Matlab from using old file handles? I tried looking through the settings, but nothing seems relevant.
I'm running Windows XP, 64 bit, with 8 Gbytes of RAM. ver reports the Matlab and Windows versions as MATLAB Version 184.108.40.2069 (R2010a) Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)
I was also experiencing this behavior when using run() to execute a script used as an input file. I found in the Matlab documentation for the run() function the reason for this:
"If a script is not on the MATLAB path, executing the run command caches the script. In the same session and after calling run, you can edit the script using an external editor. Call clear scriptname before calling run again to use the changed version of the script rather than the cached version. If you edit the script with the MATLAB editor, run executes the changed version and there is no need to call clear scriptname."
I've verified that the following works:
script = 'C:\some_path\do_stuff.m';
I tried using cd() and addpath() but was never able to get Matlab to ignore the cached copy without using clear().
Additionally, my initial workaround was eval(fileread(filepath)) which worked for my use case. This evaluates the text in the file directly without giving Matlab the chance to cache the file. Since run() calls evalin(), neither completely avoids the eval functions. I haven't done any performance testing to know which is faster or better, but the clear(), run() method looks cleaner.
I also verified that I only experienced this problem when saving with external text editors, even if I had the file up in Matlab as well (which auto-loads the externally saved version). Only if you save the file with the Matlab editor will Matlab clear it's cached copy automatically.
the cyclist 2012-2-14
One thing you could try would be to do
(before/after you run filename.m) to see if it is running the code from the location that you expect, and are editing.
Ryland Mathews 2019-7-2
I have the same issue but with .py file. If i change the file I literally have to restart to get it not to use a chached version.
Simon Robinson 2020-6-2
For me, the problem arises sometimes when debugging, and the solution is to use the 'Quit debugging' button in the Editor tab and run again to the same point.
(The function was in the MATLAB path and being edited and saved within MATLAB, but it was clear from stepping through and the placement of stops that MATLAB wasn't debugging the edited version).