Attempt to execute SCRIPT ... as a function:

71 visualizaciones (últimos 30 días)
Armin Schindler
Armin Schindler el 20 de Nov. de 2020
Editada: Cris LaPierre el 21 de Nov. de 2020
I am very new to MatLab and this is probably a really easy fix but functions are giving me a headache.
global fakt1 fakt2 fakt3
fakt1=2; fakt2=3; fakt3=4;
testfunktion1(10)
function erg=testfunktion1(x)
erg=fakt1*x^3+fakt2*x^2+fakt3*x;
end
I first named the file "testfunktion.m" but then got the Error "Local function name must be different from the script name". After changing the function name and trying to run the script I get the Error
">> testfunktion(10)
Attempt to execute SCRIPT testfunktion as a function: C:\Users\....."
Now I am kind of clueless on what to do.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Nov. de 2020
Editada: Cris LaPierre el 21 de Nov. de 2020
The issue is that you are treating your script as a function. Functions have inputs, scripts do not.
>> testfunktion(10)
Just call your script without trying to give it an input.
>> testfunktion
As is written, that will create an error about missing variable x. So either turn the entire script into a function, or define x in your script as well.
As far as learning more about MATLAB, I recommend MATLAB Onramp. Also, rather than declare your variables a global, just pass them as inputs to your function. See more here.
  1 comentario
Armin Schindler
Armin Schindler el 21 de Nov. de 2020
Thanks for the advice, I got it working now. The declaration of global variables was just part of the task I was given.

Iniciar sesión para comentar.

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 20 de Nov. de 2020
What do you mean by '10' on this line
testfunktion(10)
To call a script, you just need to type the script name
testfunktion
Or you can press the big green "Run" button.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by