MATLAB Answers

Change the code when it's running

126 views (last 30 days)
If a matlab script is running, in what situations the modifications of the code will take effects immediately for the running script?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Mar 2016
That is unknown, or at least undocumented, and the answer can depend upon the MATLAB version. It depends in part on how effective Just In Time (JIT) has been for scripts, taking into consideration that for scripts it was historically considered valid to invoke something that creates new variables or redefines variables to have very different meanings. More recent versions of MATLAB (R2015a I think it was) have made explicit that if a variable with the same name as a function is "poofed" into existence that the function will still be referred to. This implies some amount of parsing and buffering that was not present before, but gives no information about when the file might be checked.
There are some hints that MATLAB might potentially read from the file after executing each line. There are other hints that MATLAB might potentially read from the file after executing all of the top level commands started on a line. But those "potentially" are not guarantees.
Historically, if a .m script was updated while it was running, then because the line boundaries might not line up, MATLAB could end up reading into what is now the middle of a line when it thinks it is about to read from the beginning of a line.
Changing the very .m script that is running is not guaranteed to be stable.
The answer is different for functions, and the answer is different if the question is about writing a .m that is not currently in execution and one wants to be sure that the new version is used.

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 14 Mar 2016
I don't think that's possible. The "edit and continue" functionality like you have in Microsoft Visual Studio languages does not (yet) exist in MATLAB.

  3 Comments

Ray Lee
Ray Lee on 14 Mar 2016
at least, the callback function of a figure window is real-time up-to-date.
Image Analyst
Image Analyst on 14 Mar 2016
I don't have any idea what that means.
Walter Roberson
Walter Roberson on 14 Mar 2016
Once a function has been executed, it will be parsed and the parsed version may (and often is) used in preference to changes in the file. If the function encountered any persistent variables then the parsed version will certainly be retained indefinitely. The parsed version of functions without persistent is indefinite, perhaps until memory gets scarce. However, deliberately clearing the function will discard the parsed version. rehash requests can play a role in discarding parsed versions. When the command line is returned to, checks for updates to some locations are done. Note that a gui typically returns to the command line while waiting for user actions in which case the code is not executing.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by