Function name has to be same as file name?

Hi, I have a .m file called randomStep, and within the file I want to have a function called randomStep. However, when I name the function randomStep, it isn't running the program but giving me an error saying: Function 'randomStep' has already been declared within this scope. When I change the name to randomStep2 it works fine and runs the program. When creating functions, aren't the functions supposed to have the same names as the file? I also want to be able to call the randomStep function in another file, which isn't working for me. Any help would be appreciated greatly. Below is a copy of my randomStep function, which is saved inside a file called randomStep.m:
[x,y]=randomStep2([103.2, 203.1, 500],[45.2, 0.1, 20],400,400,20)
function [x,y] = randomStep2(x,y,xMax,yMax,speed)
N=normrnd(0,1,1,length(x));
for i=1:length(x)
if(x(i)+N(i)*speed)>xMax
x(i)=xMax-mod(x(i),xMax);
elseif(x(i)+N(i)*speed)<0
x(i)=abs(x(i)+N(i)*speed);
else
x(i)=x(i)+N(i)*speed;
end
end
N=normrnd(0,1,1,length(y));
for j=1:length(y)
if(y(j)+N(j)*speed)>yMax
y(j)=yMax-mod(y(j),yMax);
elseif(y(j)+N(j)*speed)<0
y(j)=abs(y(j)+N(j)*speed);
else
y(j)=y(j)+N(j)*speed;
end
end
disp([x])
disp([y])
end

1 commentaire

Leo Stocco
Leo Stocco le 17 Déc 2022
Is there any way to force Matlab to generate a warning message when it runs a function does not have the same name as the filename? This causes me problems when I accedentally misname a function since it can sometimes get "found" somewhere else in my matlab search path and cause unexpected results that can be really tricky to debug.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 9 Mai 2020
Modifié(e) : Matt J le 9 Mai 2020
When creating functions, aren't the functions supposed to have the same names as the file?
No. If your mfile is a function file, then it is good practice (but not necessary) for the top level function to have the same name as the file. Your mfile is in any case not a function file. It is a script,
within which you have attempted to define randomStep as a local function. A local function should not have the same name as the file.
For an mfile to qualify as a function file, then its only purpose should be to define a function and the first line in the file must declare the input/output syntax of that function:
function [x,y] = randomStep(x,y,xMax,yMax,speed)

5 commentaires

Khadeja Khan
Khadeja Khan le 9 Mai 2020
Ok so should I keep the name as randomStep 2? I am trying to access this function in another m file by calling:
[x,y]=randomStep2([103.2, 203.1, 300, 303.3, 244, 92, 37, 222, 299, 111],[45.2, 0.1, 20, 99, 108, 268, 399, 254, 77, 53],400,400,20)
in that file, but it keeps giving me this error:
Unrecognized function or variable 'randomStep2'.
Error in singleSimulation (line 3)
[x,y]=randomStep2([103.2, 203.1, 300, 303.3, 244, 92, 37, 222, 299, 111],[45.2, 0.1, 20, 99, 108, 268, 399, 254, 77, 53],400,400,20)
Matt J
Matt J le 9 Mai 2020
Modifié(e) : Matt J le 9 Mai 2020
I recommend you name everything as you did originally, but move the first line to another file
[x,y]=randomStep([103.2, 203.1, 500],[45.2, 0.1, 20],400,400,20) %move this to another file
so that the first line of randomStep.m will instead be,
function [x,y] = randomStep(x,y,xMax,yMax,speed)
By doing this, randomStep.m will become a function file and you can call the randomStep function from anywhere in the same folder. Or, you can put that folder on your Matlab search path and call the function from anywhere you want.
Khadeja Khan
Khadeja Khan le 9 Mai 2020
Thank you! That worked :)
What if I want to have a function like:
function [sSeries, iSeries, rSeries]=singleSimulation(populationSize, xMax, yMax, timeSteps, speed, radius, recoveryProb)
but I also want its outputs of [sSeries, iSeries,rSeries] to be within the same m file as where the function is defined? Should the function be a different name to the m file in that case?
Matt J
Matt J le 9 Mai 2020
Thank you! That worked :)
You're welcome. Since it did work, though, please Accept-click the answer.
but I also want its outputs of [sSeries, iSeries,rSeries] to be within the same m file as where the function is defined?
Then you would make singleSimulation a local function inside that mfile. However, singleSimulation will then only be invokable within that file and not elsewhere. (Although, you could create a function handle to singleSimulation and use that to invoke the function indirectly).
Should the function be a different name to the m file in that case?
Yes.
Khadeja Khan
Khadeja Khan le 10 Mai 2020
Hey Matt, I have another seperate question which I posted about my loops not working correct, and it seems like I need my r count to not decrease to prevent those who are recovered from being re infected. Could you please take a look at it and possibly give some suggestions about how I may implement such an r count? Thanks so much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by