MATLAB Answers

Cannot have function in script, Matlab 2016b

21 ビュー (過去 30 日間)
ML
ML 2017 年 5 月 10 日
コメント済み: ML 2017 年 5 月 11 日
Hi!
I'm trying to run a script with a function declared in the bottom. This should work on 2016b version, but for me it doesn't. I have version 9.1.0.441655 (R2016b).
Why does this not work?
  2 件のコメント
ML
ML 2017 年 5 月 10 日
Sorry... I get the same message as earlier versions. "The selected section cannot be evaluated because it contains a invalid statement" and in console: "Function definitions are not permitted in this context.".
This is the same message as in earlier versions if I try to find a solution on the internet. However, I allready have the 2016b version, in which it should work?

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

採用された回答

Steven Lord
Steven Lord 2017 年 5 月 10 日
Are you trying to copy a segment of the script that includes the function definition and paste it into the Command Window, or right-clicking and selecting "Evaluate Selection"? That will not work, any more than pasting the code starting with the line that declares the function using the function keyword to the Command Window would. From the Release Notes in release R2016b for this feature:
" Note: If your script contains local functions, you cannot run code sections (also known as code cells) individually. The Run Section and Run and Advance buttons are disabled. To run the script, type the saved script name in the Command Window or click Run. You can run sections individually in live scripts containing local functions. "
  1 件のコメント
ML
ML 2017 年 5 月 11 日
Yes, this was the correct answer. I thought that you could run code sections with local functions in the end, if the function was declared in the same section. "Run" seems to work just fine. Thanks for your explanation!

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by