Change the code when it's running

조회 수: 42 (최근 30일)
Ray Lee
Ray Lee 2016년 3월 14일
댓글: Walter Roberson 2016년 3월 14일
If a matlab script is running, in what situations the modifications of the code will take effects immediately for the running script?

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 14일
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.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 3월 14일
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
Image Analyst
Image Analyst 2016년 3월 14일
I don't have any idea what that means.
Walter Roberson
Walter Roberson 2016년 3월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by