External changes in script files not updated when run

23 views (last 30 days)
I have experienced an undesired behavior when a MATLAB script "function.m" calls a different script "parameter_file.m" which is edited between calls:
If a value in the "parameter_file.m" file is edited in MATLAB, then the changed value will be used when running the "script.m" the next time.
However, if a different text editor is used (outside of MATLAB) to change the file, then MATLAB loads the previous version with the old value for "my_parameter".
How can this be adapted, so that always the current version is interpreted?
You can test the behavior by running the attached script.m file and changing the value in the "parameter_file.m" file from within and outside of MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jul 2020
This is due to the way the "run" function caches files. From the documentation for the "run" function:
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.
The specific workaround to the attached script is to put this code on the first line of "script.m":
>> clear parameter_file
  1 Comment
Matt Cooper
Matt Cooper on 7 Oct 2022
Did this behavior change between R2020b and R2021b? I get different behavior on two separate computers running these two versions, respectively. On R2020b, I can run a script in one instance, then edit that same script (in the Matlab editor) in a second instance and run it, and the two instances use the script as it existed at run time. In R2021b, when the script is edited in the second instance, the first instance appears to recognize the changes.
I don't see anything obvious in the release notes, other than possible side effects related to the backgroundPool function. Please let us know how we can find information about this in general (outside of release notes) to diagnose this behavior. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!