Local functions are not working in MatLab R2022a

42 visualizaciones (últimos 30 días)
Jonne Klockars
Jonne Klockars el 9 de Jun. de 2022
Editada: Walter Roberson el 27 de Ag. de 2024
I'm trying to write local functions in the way instructed in this video: https://www.youtube.com/watch?v=f0zKcuP3Wc0&list=PLlvaivyRxHmdTvpNBe4gKWrOBrcuxhpsl&index=48, beginning at 5:50. When running the code below, my MatLab R2022a says Unrecognized function or variable 'mymean'. What am I missing here?
x = 1:10;
n = length(x);
avg = mymean(x,n)
function a = mymean(v,n)
a = sum(v)/n;
end
  6 comentarios
Stephen23
Stephen23 el 9 de Jun. de 2022
@Jonne Klockars: how are you running the script? Please show the exact code you use to run your file.
Jonne Klockars
Jonne Klockars el 9 de Jun. de 2022
The exact code is in the question. I have nothing else in the file. I select it all and press F9.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 9 de Jun. de 2022
Editada: Stephen23 el 9 de Jun. de 2022
"I select it all and press F9."
That will not work.
You need to run the script by calling it by name from the command line:
>> test
MATLAB is a bit more strict with function definitions than some younger languages around these days, where you can just define anything on the fly. Selecting function code and pressing F9 will not define the function and then let you call it. MATLAB simply does not work that way. You need to call the function or run the script that calls the function.
  3 comentarios
Jonne Klockars
Jonne Klockars el 9 de Jun. de 2022
Also selecting the green play-symbol (Run) works. I watched the video series (link in question) before beginning the exercises and saw the instructor using F9. I'm quite sure she doesn't mention about the difference between it and Run / Command Line in any of the 60 videos.
Yu Li
Yu Li el 27 de Ag. de 2024
some keyboard include a bottom "fn", when you accidently click that bottom, F9 not work. you need to press "fn" buttom agian to recover that.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by