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

1 visualización (últimos 30 días)
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 comentarios
Mhahida Baksh
Mhahida Baksh el 23 de Abr. de 2020
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 el 23 de Abr. de 2020
"...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.

Iniciar sesión para comentar.

Respuestas (2)

Srivardhan Gadila
Srivardhan Gadila el 29 de Abr. de 2020
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 comentario
Steven Lord
Steven Lord el 16 de Mayo de 2021
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.

Iniciar sesión para comentar.


Mubbara Muhammad Saleem Sheikh
Mubbara Muhammad Saleem Sheikh el 16 de Mayo de 2021
Editada: Rik el 7 de Oct. de 2021
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 comentarios
Mubbara Muhammad Saleem Sheikh
Mubbara Muhammad Saleem Sheikh el 29 de En. de 2022
Yeah i write function in file "tictac.mm" like this. This is code file
Mubbara Muhammad Saleem Sheikh
Mubbara Muhammad Saleem Sheikh el 29 de En. de 2022
After this , where we apply function write any value of functions u will get value

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by