MATLAB Answers

Stale files used instead of updated files

57 views (last 30 days)
Michael
Michael on 14 Feb 2012
Answered: Simon Robinson on 2 Jun 2020
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)

  0 Comments

Sign in to comment.

Answers (5)

Brett
Brett on 9 Jan 2015
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 Comments

Lee
Lee on 23 Nov 2017
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 on 24 Nov 2017
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.

Sign in to comment.


the cyclist
the cyclist on 14 Feb 2012
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.

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Feb 2012
After you save the function, give the command
rehash

  4 Comments

Show 1 older comment
the cyclist
the cyclist on 14 Feb 2012
Walter, I'm curious as to your hypothesis here. I mean, one shouldn't have to rehash (every time?!) after simply saving a file. Do you have thoughts on what is going wrong?
Walter Roberson
Walter Roberson on 14 Feb 2012
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 on 9 Jan 2015
I tried rehash and it didn't work for me.
clear(script) worked. Further details are in my answer.

Sign in to comment.


Ryland Mathews
Ryland Mathews on 2 Jul 2019
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.

  0 Comments

Sign in to comment.


Simon Robinson
Simon Robinson on 2 Jun 2020
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).

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!