MATLAB Answers

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

376 views (last 30 days)
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:
function [any, tri, sqr, rect] = main(number_of_sticks)
%This function will be used for finding if we can construct
%equilateral triangle/square/rectangle
any=(tri==1 || sqr==1 || rect==1);

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 2 Mar 2021
Edited: Stephen Cobeldick on 2 Mar 2021
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.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by