functions within the script

4 visualizaciones (últimos 30 días)
Rashid Hussein
Rashid Hussein el 27 de Jun. de 2021
Comentada: Rashid Hussein el 27 de Jun. de 2021
i wanted to call the same function several times but with different variables eachtime in the same script
for example
function [a]=myfunction(r)
a=10*r
end
a=myfunction(5)
a=myfunction(7)
i want when run the script to have all results at once but it shows this error message
Error: File: myhot.m Line: 5 Column: 2
This statement is not inside any function.
(It follows the END that terminates the
definition of the function "myfunction".)
thankyou
  2 comentarios
Akshit Bagde
Akshit Bagde el 27 de Jun. de 2021
If you are writing a script file which contains both commands and function definitions, the functions must be placed at the end of the file. Read about it here - Declare Functions in MATLAB.
Rashid Hussein
Rashid Hussein el 27 de Jun. de 2021
Thank you sir for answering But I want to call the function in the script not in the command line , so when I run the script it will give me the results If it is possible, can you demonstrate the above example given by me ?

Iniciar sesión para comentar.

Respuesta aceptada

Stephan
Stephan el 27 de Jun. de 2021
Editada: Stephan el 27 de Jun. de 2021
a1=myfunction(5)
a2=myfunction(7)
function a=myfunction(r)
a=10*r
end
  2 comentarios
Star Strider
Star Strider el 27 de Jun. de 2021
Relevant documentation: Create Functions in Files
Rashid Hussein
Rashid Hussein el 27 de Jun. de 2021
Thank you sir for your answer , appreciate your efforts

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 27 de Jun. de 2021
Put the script first, not after the function. And make sure the function ends with an "end" statement.
  1 comentario
Rashid Hussein
Rashid Hussein el 27 de Jun. de 2021
Thank you sir for your answer, deeply appreciated

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB 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