Stale files used instead of updated files

조회 수: 18 (최근 30일)
Michael
Michael 2012년 2월 14일
답변: Simon Robinson 2020년 6월 2일
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에서 Search Path에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by