what does this error means-"Function definitions are not permitted at the prompt or in scripts."?

 채택된 답변

Thorsten
Thorsten 2015년 11월 18일
편집: Thorsten 2015년 11월 18일
To define a function, create a new m-file on the command line
>> edit myfun.m
write your function in this file, first non-command line must contain the function header using the "function" keyword, like function y = myfun(x). Save it, and call it from the command line or within another function or within a script
myfun(123)
You've probably created a file with some matlab commands, i.e., a script, and somewhere below in this script you defined a function. That's not allowed. Either move your function to a separate file, or convert your script to a function by adding a first line like
function mycommands
Now it's a function, and you can define a function within this function. However, such a nested function is visible only within this function, not at the command line and not within other functions. And functions can be nested only one level, but there can be multiple nested functions.

추가 답변 (2개)

Meghana Dinesh
Meghana Dinesh 2015년 11월 18일
편집: Meghana Dinesh 2015년 11월 18일
It means you are trying to define a function in the prompt or maybe within a different script. This is not permitted in MATLAB. Define functions in their respective m-files (save the m-file with the same name as the function's). Read more about MATLAB functions .

댓글 수: 5

Lines that start with the word "function" cannot be used at the matlab prompt. They can only be used stored in files, and those files need to have either "function" or "classdef" as their first non-comment part.
It means one needs to define the function separately not in the same matlab program. If so then how to call it?
One MATLAB M-file can contain either
  1. one script
  2. one or more functions
It is not possible to mix the two: a file either is a script, or has functions, but cannot have both. I would recommend that you learn to write code using functions, which have many advantages over scripts.
  1. call a script by using the filename.
  2. call a function by using the function name.
thank you stephen and thorsten.
Is it possible to run function program directly?

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

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

태그

질문:

2015년 11월 18일

답변:

2015년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by