Function name has to be same as file name?
Afficher commentaires plus anciens
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
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.
Réponse acceptée
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!