Function name has to be same as file name?
223 views (last 30 days)
Show older comments
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 Comment
Leo Stocco
on 17 Dec 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.
Accepted Answer
Matt J
on 9 May 2020
Edited: Matt J
on 9 May 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 Comments
Matt J
on 9 May 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.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!