MATLAB Answers

Why do I receive the error "Attempt to execute SCRIPT FILENAME as a function"?

3,093 views (last 30 days)
I receive the error:
??? Attempt to execute SCRIPT untitled as a function.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Oct 2013
Edited: MathWorks Support Team on 12 Feb 2018
This error results because there is a MATLAB script named [filename] and your calling syntax requires that this script either takes arguments or returns values, which are properties of a function and not of a script. For example, if you have a script called 'banana.m' containing:
disp('banana');
and you call the script with two input arguments as though it were a function by typing the following at the Command Prompt:
banana(1,1)
Then you will get the error. To call the script without error, try calling the script without arguments:
banana
Second, the error can happen because there is a script [filename] and a function [filename] that both exist on the path. If the script appears before the function on the path, then your commands will attempt to use it instead of the function. In Release 2006b (R2006b) and newer versions of MATLAB, the error message will display the location of the script that is called. Rename this script to a more appropriate name. For previous product releases you may use the command:
which <filename>
to determine which instance of [filename] that MATLAB is attempting to call. If you want to see all instances of a function on the path, type the command:
which <filename> -all
Alternatively, such behavior may occur in certain scenarios where your filename contains invalid characters. In particular, you may see this error if the filename contains characters that are MATLAB operators such as -, +, or *. MATLAB filenames must begin with a letter, which may be followed by any combination of letters, digits, and underscores.

  9 Comments

Show 6 older comments
zakria qadir
zakria qadir on 13 Aug 2020
I am getting this Error: anyone who can solve this/=?
Attempt to execute SCRIPT newplot as a function:
D:\MATLAB\R2017a\toolbox\matlab\graphics\newplot.m
Error in contour (line 66)
cax = newplot(cax);
Error in PSO_simulation (line 19)
contour(x_new, y_new, z , 20);
>>
Walter Roberson
Walter Roberson on 13 Aug 2020
I think you might have accidentally edited the file and added some characters before the "function" keyword

Sign in to comment.

More Answers (11)

Lode
Lode on 24 Jan 2016
Maybe a stupid remark, but it took me 1h to find...
run(filename.m) doesn't work. You should use run('filename.m') if you didn't put the filename in a variable. This is a very basic mistake but maybe useful to somebody...

  3 Comments

Sign in to comment.


Bruce Lin
Bruce Lin on 2 Nov 2015
As mentioned in my comment to JMS above, there is a different way you can get this error which the Mathworks respondents did not cover.
If you have a script called ABC.m and a script called ABC-copy.m, and try to run ABC-copy, it will fail with the error above "Attempt to execute SCRIPT FILENAME as a function"
MATLAB interprets the minus sign as an operator, not part of a filename, and the fact that you ALSO have a script called "ABC.m" means that MATLAB tries to execute
"ABC.m - copy.m"
You can prove this by temporarily deleting ABC.m and running 'ABC-copy'. You will see that you get a file not found error instead where MATLAB cannot find ABC.m
One would think that this could be corrected by executing 'ABC-copy.m', i.e. explicitly specifying the .m extension, but it doesn't fix the problem for me.
Hope this helps someone in the future.

  3 Comments

Steven Lord
Steven Lord on 2 Nov 2015
Function names in MATLAB must:
  • Start with a letter
  • Consist only of letters, numbers, and the underscore character
  • Not be a keyword (see the output of ISKEYWORD)
  • Be no more than NAMELENGTHMAX characters in length.
The function name "ABC-copy" violates the second of those restrictions. I would probably rename that function to "ABC_copy" instead.
Mohsin Shah
Mohsin Shah on 11 Dec 2017
I faced the same error and I fixeded it by following your answer. Thank you so much.

Sign in to comment.


Walter Roberson
Walter Roberson on 4 May 2015
In addition:
As a practical matter, when basic built-in routines such as disp() are the ones being mentioned as scripts, the problem is almost always that the installation is corrupt for any of several reasons, and reinstallation is required.
A lesser possibility is that the installation is okay but the user has managed to set the MATLAB path to include library directories that should not be named directly, such as if the documentation directory has been added to the MATLAB path. In such cases, restoredefaultpath can be used.

  0 Comments

Sign in to comment.


Hugo Pontes
Hugo Pontes on 14 May 2020
I'm just answering so that if anyone faces the same problem I did it'll easier to correct.
If you download the same .m file twice to windows it will automatically name it filename(1).m . If you try to run that file with the (1) MATLAB will assume it's a function and that error will prompt. It sounds silly but yes just change the file name!

  0 Comments

Sign in to comment.




Ivan Dwi Putra
Ivan Dwi Putra on 27 Nov 2019
I can't execute the lqr function
close all
% Initial Conditions
x0 = [3; % 3 radians
0]; % 0 rad/s
% System Dynamics
A = [0 1;
0.01 0];
B = [0;
1];
C = [1 0];
D = 0;
% Control Law
Q = [1 0; % Penalize angular error
0 1]; % Penalize angular rate
R = 1; % Penalize thruster effort
K = lqr(A,B,Q,R);
% Closed loop system
sys = ss((A - B*K), B, C, D);
% Run response to initial condition
t = 0:0.005:30;
[y,t,x] = initial(sys, x0, t);

  0 Comments

Sign in to comment.


ismet iris
ismet iris on 24 May 2020
comPort = 'COM7';
if (~exist('serialFlag','var'))
[arduino.s,serialFlag]=setupSerial(comPort);
end
if(~exist('h','var') || ~ishandle(h))
h=figure(1);
end
if(~exist('text1','var'))
text1=uicontrol('Style','Text','String','X: 0 degrees',...
'pos',[450 100 100 25],'parent',h);
end
if(~exist('text2','var'))
text2=uicontrol('Style','Text','String','Y: 0 degrees',...
'pos',[450 75 100 25],'parent',h);
end
if(~exist('button','var'))
button=uicontrol('Style','togglebutton','String','Stop & Close Serial Port',...
'pos',[0 0 200 25],'parent',h);
end
weights = [ 0 20 50 70 100 120 150 170 200 220 250];
m = zeros(length(weights),1);
for i = 2:length(weights)
mbox=msgbox(['Place' num2str(weights(i)) ' grams on thr FSR']); uiwait(mbox);
m(i)=readFSR(fsr);
while(m(i) < m(i-1)) || m(i) ==0
m(i) = readFSR(fsr);
end
end
p1=polyfit(m,weights,2);
myaxes=axes('xlim',[-20 20], 'ylim', [-20 20],'zlim',[-0 250]);
view(3);
grid on;
axis equal;
hold on;
[xsphere, ysphere, zsphere]=sphere();
h(1)=surface(xsphere, ysphere, zsphere);
combinedobject = hgtransform('parent',myaxes);
set(h, 'parent',combineobject)
drawnow;
while (get(button, 'value')==0)
[voltage]=readFSR(fsr);
mass = polyval(p1, voltage);
if (mass>0)
force=mass*9.81;
set(text1,'String',['Mass :' num2str(round(mass)) 'grams'])
set(text2,'String',['Force :' num2str(round(force)) 'N'])
translation = makehgtform('translate',[0 0 mass]);
set(combinedobject,'matrix',translation);
scaling=makehgtform('scale',mass/10);
set(combinedobject,'matrix',scaling);
set(combinedobject, 'natrix',translation*scaling);
end
pause(0.1);
end
closeSerial;
WHY THİS ERROR ?
Attempt to execute SCRIPT fsr as a function:
C:\Users\ismet\Desktop\fsr.m
Error in fsr (line 28)
m(i)=readFSR(fsr);

  1 Comment

Walter Roberson
Walter Roberson on 13 Jun 2020
this is related to http://www.matlabarduino.org/force.html
The original code assigns the results of setting up the serial port to a variable named fsr.s but your code assigns to arduino.s but still expects fsr variable to have been assigned to.

Sign in to comment.


Anish Gunjal
Anish Gunjal on 4 Jul 2020

in my case, I got an error "Attempt to execute SCRIPT FILENAME as a function"? because one of my FILENAME in the folder is having the same name as a FUNCTION i was executing. I just renamed the FILENAME and the error was solved.

  0 Comments

Sign in to comment.


ahsan meo
ahsan meo on 22 Nov 2020
clc
close all
clear all
x=[1 2 3];
y=[4 5 6];
c=polyfit(x,y,2)
this error will be come
Attempt to execute SCRIPT polyfit as a function:
D:\NC codes\polyfit.m
Error in vander (line 6)
c=polyfit(x,y,2)
>>

  1 Comment

Steven Lord
Steven Lord on 22 Nov 2020
You have written your own polyfit.m script file that takes precedence over the polyfit function included in MATLAB. Rename your script to something that is not already the name of a function in MATLAB (mypolyfit.m is a pretty safe choice.)

Sign in to comment.


ahsan meo
ahsan meo on 25 Nov 2020
clc
close all
clear all
x =[-2, -1, 2];
A = vander([-2, -1, 2]);
y = [-2 1 -1]';
c = A\y
these errors are come
Attempt to execute SCRIPT vander as a function:
D:\NC codes\vander.m
Error in Untitled111 (line 5)
A = vander([-2, -1, 2]);

  4 Comments

Show 1 older comment
ali syal
ali syal on 4 Jan 2021
Please help
Function dx= backstepping(t,x)
z=x2-(-c1(x1));
x1dot=z-c1(x1);
zdot=u+c1(z-c1(x1));
x2=z-c1(x1);
x1=1;
c1=1;
v(x1,z)=x1^2/2+z^2/2;
vdot(x1,z)=-c1(x1^2)-z^2;
u=((c1^2)*(x1))-x1-(x2+(c1)*(x1)*(c+1));
dx=x1dot+zdot;
[t,x]=ode45(@backstepping,[1,10],2);
plot(t,x)
grid on
backstepping_s
Attempt to execute SCRIPT backstepping as a function:
/MATLAB Drive/backstepping.m
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in backstepping_s (line 1)
[t,x]=ode45(@backstepping,[1,10],2);
Steven Lord
Steven Lord on 4 Jan 2021
In addition to Walter's correction, don't try to call ode45 with @backstepping as its first input from within backstepping.m itself.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!