Live script section breaks removes import

7 ビュー (過去 30 日間)
Eugenio Grabovic
Eugenio Grabovic 2021 年 10 月 14 日
コメント済み: Eugenio Grabovic 2021 年 10 月 25 日
In a live script notebook, i f i import a package and add a section break, the import is removed. Is it intended to work that way?
Example:
import casadi.*
***add a section break here ***
x = SX.sym('x');
if i run the first section and then the second one, the first time it works without any issues; but if i try to add to the second section:
x = SX.sym('x');
y = SX.sym('y');
it prduces the error "Unable to solve the name SX.sym" and if i type "import" in the command window it shows the casadi package has been somehow removed, focing me to always rerun the import section

採用された回答

Arthi Sathyamurthi
Arthi Sathyamurthi 2021 年 10 月 25 日
編集済み: Arthi Sathyamurthi 2021 年 10 月 25 日
The import function adds package, class, or functions to the current import list. The import list scope for a script is the entire script body. The imports in a script are available only in the script body and are not available in the scopes which call the script. Hence executing a script/live script with a particular set of code/sections, allows only that code/sections to have access to that import list scope. And any new modifications/ addition of new sections is not available in the scopes which call the script and gives an error.
You can know more about the scopes of the import function in the link here.
  1 件のコメント
Eugenio Grabovic
Eugenio Grabovic 2021 年 10 月 25 日
Thank you for the answer, however this feels counterintuitive: if all variables are accessible by any sections of the script, the imports should be as well (imho).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScripts についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by