MATLAB Answers

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

3,124 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:
and you call the script with two input arguments as though it were a function by typing the following at the Command Prompt:
Then you will get the error. To call the script without error, try calling the script without arguments:
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.
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 (12)

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...

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.

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.

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!

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;
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);

ismet iris
ismet iris on 24 May 2020
comPort = 'COM7';
if (~exist('serialFlag','var'))
if(~exist('h','var') || ~ishandle(h))
text1=uicontrol('Style','Text','String','X: 0 degrees',...
'pos',[450 100 100 25],'parent',h);
text2=uicontrol('Style','Text','String','Y: 0 degrees',...
'pos',[450 75 100 25],'parent',h);
button=uicontrol('Style','togglebutton','String','Stop & Close Serial Port',...
'pos',[0 0 200 25],'parent',h);
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);
while(m(i) < m(i-1)) || m(i) ==0
m(i) = readFSR(fsr);
myaxes=axes('xlim',[-20 20], 'ylim', [-20 20],'zlim',[-0 250]);
grid on;
axis equal;
hold on;
[xsphere, ysphere, zsphere]=sphere();
h(1)=surface(xsphere, ysphere, zsphere);
combinedobject = hgtransform('parent',myaxes);
set(h, 'parent',combineobject)
while (get(button, 'value')==0)
mass = polyval(p1, voltage);
if (mass>0)
set(text1,'String',['Mass :' num2str(round(mass)) 'grams'])
set(text2,'String',['Force :' num2str(round(force)) 'N'])
translation = makehgtform('translate',[0 0 mass]);
set(combinedobject, 'natrix',translation*scaling);
Attempt to execute SCRIPT fsr as a function:
Error in fsr (line 28)
  1 Comment
Walter Roberson
Walter Roberson on 13 Jun 2020
this is related to
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.

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

Ritu Roy Chowdhury
Ritu Roy Chowdhury on 16 Mar 2021 at 15:55
Also make sure that the name of the script doesn't include hyphens (-), and other illegal characters.

Community Treasure Hunt

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

Start Hunting!