MATLAB Answers

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

2,491 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
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 (15)

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

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

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.


ahsan meo
ahsan meo on 22 Nov 2020
Edited: Walter Roberson on 3 Jul 2021
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
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
Hello,
Also make sure that the name of the script doesn't include hyphens (-), and other illegal characters.
Best

madhuri parimi
madhuri parimi on 29 Jun 2021
n=1000;
p=[];
for i=1:n
phase(i)=(pi/n)*(i-1)*(i-n-1);
end
for k=1:1000;
y(k)=sqrt(cos(phase(k)^2)+(sin(phase(k)^2)));
end
x1 autocorr = autocorr(y);
x1=x1 autocorr^2;
x2=max( x1 autocorr);
s=0;
for i=1:20
s=s+((x1 autocorr(i)^2)/(x2^2));
end
islr=10*log10(s/150)
for j=5:20
x4=(x1 autocorr(j));
end
pslr=20*log10(0.3020/(3*x2))
it is showig error in autocorr function can u help me with that?
  4 Comments

Sign in to comment.


madhuri parimi
madhuri parimi on 30 Jun 2021
n=1000;
p=[];
for i=1:n
for j=1:n
phase(i)=(pi/2*n)*(n-2*i+1)*(n-2*j+1);
end
end
for k=1:1000
for l=1:1000
y(k)=sqrt(cos(phase(k)^2)+(sin(phase(k)^2)));
end
end
M = 20;
R = zeros(M+1, 1);
for n=0:M
for k=0:n-1
R(n+1) = R(n+1) + y(k+1) * y(k+n+1);
end
end
x1autocorr = R(n+1);
x1=x1autocorr^2;
x2=max(x1autocorr);
s=0;
for i=1:20
s=s+((x1autocorr(i)^2)/(x2^2));
end
islr=10*log10(s/150)
for j=5:20
x4=(x1autocorr(j));
end
pslr=20*log10(0.3020/(3*x2))
error:
Index exceeds the number of array elements (1).
Error in final (line 25)
s=s+((x1autocorr(i)^2)/(x2^2));
help me to rectify it
  1 Comment
Walter Roberson
Walter Roberson on 30 Jun 2021
n=1000;
p=[];
for i=1:n
for j=1:n
phase(i)=(pi/2*n)*(n-2*i+1)*(n-2*j+1);
end
end
for k=1:1000
for l=1:1000
y(k)=sqrt(cos(phase(k)^2)+(sin(phase(k)^2)));
end
end
M = 20;
R = zeros(M+1, 1);
for n=0:M
for k=0:n-1
R(n+1) = R(n+1) + y(k+1) * y(k+n+1);
end
end
R is a vector at this point
x1autocorr = R(n+1);
Here you are using the "left-over" value of n from the for n loop when you do the indexing. The last value assigned to n is going to be M so after the for n loop, n is going to equal M, So R(n+1) is going to be R(M+1) . If that is your intention, then code R(M+1) so that people do not get confused about which part of R is being accessed.
n is going to be a scalar after the loop. It is not uncommon for people to expect that after a for loop, that the loop control variable will be a vector (or array) of all of the values the loop control variable was assigned, but that simply is not the case: after the loop it will just be the last value. So R(n+1) is going to be a scalar, so x1autocorr() is going to be a scalar.
x1=x1autocorr^2;
That line assumes that x1autocorr is a scalar (or a square matrix)
x2=max(x1autocorr);
max() of a scalar is the value of the scalar.
size(x1autocorr)
ans = 1×2
1 1
s=0;
for i=1:20
s=s+((x1autocorr(i)^2)/(x2^2));
end
Index exceeds the number of array elements (1).
You are trying to access x1autocorr(i) but x1autocorr is a scalar.
islr=10*log10(s/150)
for j=5:20
x4=(x1autocorr(j));
end
You overwrite all of x4 each iteration of that for j loop. What is the point of doing that?
pslr=20*log10(0.3020/(3*x2))
You ignore the x4 you just created, and you use x2, but x2 is not defined.

Sign in to comment.


madhuri parimi
madhuri parimi on 3 Jul 2021
Edited: Walter Roberson on 3 Jul 2021
n=1000;
p=[];
for i=1:n
phase(i)=(pi/n)(i-1)(i-n-1);
end
for k=1:1000;
y(k)=sqrt(cos(phase(k)^2)+(sin(phase(k)^2)));
end
x1autocorr = autocorr(y);
x1=x1autocorr.^2;
x2=max(x1autocorr);
s=0;
for i=1:20
s=s+((x1autocorr(i)^2)/(x2^2));
end
islr=10*log10(s/150)
for j=5:20
x4=(x1autocorr(j));
end
pslr=20*log10(0.3020/(3*x2))
error:
Attempt to execute SCRIPT error
as a function:
C:\Matlab\Matlab 2016a\Matlab
2016a\Matlab 2016a Win64
Crack\bin\error.m
Error in autocorr (line 83)
error(message('econ:autocorr:NonVectorInput'))
Error in Untitled (line 9)
x1autocorr = (autocorr(y));
help me to rectify

Community Treasure Hunt

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

Start Hunting!