"Local function name must be different from the script name" error

Hi, I wrote a simple code but I can't get output of the function because of this error. Name of the file is "main.m".
And it also says that this is a local function. Should I/how can I make it main function? The code is:
clear;
function [any, tri, sqr, rect] = main(number_of_sticks)
%This function will be used for finding if we can construct
%equilateral triangle/square/rectangle
tri=(rem(number_of_sticks,3)==0);
sqr=(rem(number_of_sticks,4)==0);
rect=(rem(number_of_sticks,2)==0);
any=(tri==1 || sqr==1 || rect==1);
end

 採用された回答

Stephen23
Stephen23 2021 年 3 月 2 日
編集済み: Stephen23 2021 年 3 月 2 日

8 投票

clear; % !!!!!!!!!! Delete this line !!!!!!!!!
function [any, tri, sqr, rect] = main(number_of_sticks) % first line of the file
Putting clear on the first line makes your file a script and causes that error (because you named the script file with the same name as the local function). Putting clear serves no purpose. Do not add things that serve no purpose.
I strongly recommend that you avoid using the variable name any.

5 件のコメント

Emre Tuzcu
Emre Tuzcu 2021 年 3 月 2 日
It worked, thanks a lot
轶文
轶文 2023 年 4 月 16 日
Why clear causes this situation?
Stephen23
Stephen23 2023 年 4 月 16 日
"Why clear causes this situation?"
My answer already explains this: "Putting clear on the first line makes your file a script...".
It is not possible (nor does it make any sense) to give both the script and the local function exactly the same name.
Note that the documentation clearly states "Script files cannot have the same name as a function in the file":
Seth
Seth 2024 年 4 月 9 日
i dont have a "clear;" this is my code,
function[yPosition, ProjectileVelocity] = projectileMotion(xPosition)
y0 = 2.0; % meters
theta = 35; % degrees
v0 = 25.0; % meters/second
g = 9.81; % m/s^2
theta = theta * pi / 180;
yPosition = xPosition * tan(theta) - (1/2) * (xPosition^2 * g) / (v0 * cos(theta))^2 + y0;
vx = v0 * cos(theta);
vy = sqrt((v0 * sin(theta))^2 - 2 * g * yPosition);
ProjectileVelocity = sqrt(vx^2 + vy^2);
end
xPosition = 25;
[yPosition, ProjectileVelocity] = projectileMotion(xPosition);
disp(['Projectile height at x = ', num2str(xPosition), ' meters: ', num2str(yPosition), ' meters']);
disp(['Projectile velocity at x = ', num2str(xPosition), ' meters: ', num2str(ProjectileVelocity), ' m/s']);
Stephen23
Stephen23 2024 年 4 月 9 日
編集済み: Stephen23 2024 年 4 月 9 日
@Seth: the problem is the code after the function definition. Either:
  • move it to a separate script, or
  • move it to before the function definition.
"Before R2024a: Local functions in scripts must be defined at the end of the file, after the last line of script code."

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 3 月 2 日

編集済み:

2024 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by