Function definitions are not permitted at the prompt or in scripts

3 vues (au cours des 30 derniers jours)
Stelios Fanourakis
Stelios Fanourakis le 17 Sep 2017
Réponse apportée : Jan le 18 Sep 2017
Can't we embed function on our script? Must be at different one and just call them?
  2 commentaires
Stelios Fanourakis
Stelios Fanourakis le 17 Sep 2017
why is that?
Stephen23
Stephen23 le 17 Sep 2017
Modifié(e) : Stephen23 le 17 Sep 2017
"Function definitions are not permitted at the prompt or in scripts"
The message seems quite clear to me: your version of MATLAB does not let you define a function inside a script, or in the command prompt. From version R2016b it is possible to define a function in a script.

Connectez-vous pour commenter.

Réponses (2)

Christoph F.
Christoph F. le 18 Sep 2017
Modifié(e) : Walter Roberson le 18 Sep 2017
You can use anonymous functions from the command line (or in a script). Compared to named functions, they are quite limited, though.
Example:
myfun = @(x) x.^3-2*x.^2+x-1
myfun(0:5)

Jan
Jan le 18 Sep 2017
Can't we embed function on our script?
Yes. Defining functions in script files was introduced in 2016b. If you have an older version, this does not work - exactly as the message tells you.
Must be at different one and just call them?
Yes. Or do not use a script file at all, but a function. In function files, the definition of subfunctions is allowed.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by