Stale files used instead of updated files

37 次查看(过去 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 7.10.0.499 (R2010a) Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)

回答(5 个)

Brett
Brett 2015-1-9
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';
clear(script);
run(script);
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.
  2 个评论
Lee
Lee 2017-11-23
Thanks Brett, this answer helped me. Observed exactly the same behavior as you and your solution is clean enough for me! IMO, this default behavior by Matlab is unacceptable, and there should at least be an option not to cache executed scripts using run command...
Walter Roberson
Walter Roberson 2017-11-24
Lee:
MATLAB caches all executed functions and scripts. This is important for performance. However if you change the .m file outside of the MATLAB editor, then it will not always notice that the file has been changed, especially if it is in another directory. You should always "clear" any .m file that you change by way of your program or through an outside means.

请先登录,再进行评论。


the cyclist
the cyclist 2012-2-14
One thing you could try would be to do
which filename
(before/after you run filename.m) to see if it is running the code from the location that you expect, and are editing.

Walter Roberson
Walter Roberson 2012-2-14
After you save the function, give the command
rehash
  4 个评论
Walter Roberson
Walter Roberson 2012-2-14
Speculation: stuck function handle.
Speculation: That particular MATLAB version was the one whose editor did not release the files.
Speculation: editing file outside of the MATLAB editor while in debug mode inside the file (or something called by the file)
Speculation: odd malfunctions come free with "The Windows Experience (TM)"
Speculation: what is needed is to "clear" the file name (without the extension), but possibly rehash will have that side effect
Brett
Brett 2015-1-9
I tried rehash and it didn't work for me.
clear(script) worked. Further details are in my answer.

请先登录,再进行评论。


Ryland Mathews
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
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).

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by