Error 'Function definition not supported in this context. Create functions in code file.'

Trying to do : function nrm = fournorm(x) for a question
But it comes up with Error: Function definition not supported in this context. Create functions in code file.

댓글 수: 5

Where are you trying to do it?
"Matlab on command window"
Not possible.
Functions may be either:
  • defined in a file with the function declaration, or
  • defined as an anonymous function.
Im trying to solve a maths question and this is what it tells me to do as the first step:
function nrm = fournorm(x) % find the four-norm of the vector x
"...this is what it tells me to do as the first step:"
You need to write that code in a file and save the file using the same name as the function, i.e. fournorm.m.
In MATLAB it is not possible to define functions like that in the command window.

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

답변 (2개)

You have to create functions in files, refer to Create Functions in Files, Add Functions to Scripts & Functions.
If you have simpler functions like
function y = cubicPoly(x)
y = x.^3 + x.^2 + x + 1;
end
then you can make use of function_handle and define such functions in command window itself or in a script as follows:
f = @(x) x.^3 + x.^2 + x + 1;

댓글 수: 1

To be clearer, you cannot define a function simply by copying and pasting the code starting with the function keyword at the prompt in the MATLAB Command Window. The function keyword is only allowed inside a script, function, or class file.

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

function move=tictac(n). %first computer move in tic-tac-toe. %go for the centre if empty. %otherwise choose the upper left corner. if n=5 move=5; else move=1 end
(Can anyone help me with this.can u tell me why matlab software shows Error: Function definition are not
supported in this context. Functions
can only be created as local or nested
functions in code files.

댓글 수: 5

Did you figure it out? I'm having a similar issue.
Yeah i made a little change in the function
what is the change , i got same issue
Yeah i write function in file "tictac.mm" like this. This is code file
After this , where we apply function write any value of functions u will get value

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2020년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by