Is it possible to set a function within one m file and use that function in matlab?

1 view (last 30 days)
In Python, we can set a function and use function at a same file.
then,Is it possible to set a function within one m file and use that function in matlab?
for example, i want to make summantion function like this,
clear all; close all; clc
N1=1; N2=100;
function summation(N1,N2)
x=0;
for ct=N1:N2
x=x+ct;
end
end
summation(N1,N2)

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2019
Yes. The function definition must be after the script calling it, and the name of the script cannot be the same as the name of the function.
  5 Comments
Walter Roberson
Walter Roberson on 16 May 2019
What is not possible in MATLAB is a function definition in the middle of a script. It is possible to define a function in the middle of a function.
function drive_my_car
N1=1; N2=100;
function summation(N1,N2)
x=0;
for ct=N1:N2
x=x+ct;
end
end
summation(N1,N2)
end
Jong Rok Lee
Jong Rok Lee on 20 May 2019
Thanks for your answer.
Now i can understand everything.
In python, The function definition must be before the script calling it so i confused.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!