functions within the script

4 次查看(过去 30 天)
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 个评论
Akshit Bagde
Akshit Bagde 2021-6-27
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 2021-6-27
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 ?

请先登录,再进行评论。

采纳的回答

Stephan
Stephan 2021-6-27
编辑:Stephan 2021-6-27
a1=myfunction(5)
a2=myfunction(7)
function a=myfunction(r)
a=10*r
end

更多回答(1 个)

Image Analyst
Image Analyst 2021-6-27
Put the script first, not after the function. And make sure the function ends with an "end" statement.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by