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

1 次查看(过去 30 天)
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 个评论
Mhahida Baksh
Mhahida Baksh 2020-4-23
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
Stephen23
Stephen23 2020-4-23
"...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 个)

Srivardhan Gadila
Srivardhan Gadila 2020-4-29
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 个评论
Steven Lord
Steven Lord 2021-5-16
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.

请先登录,再进行评论。


Mubbara Muhammad Saleem Sheikh
编辑:Rik 2021-10-7
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by