MATLAB Answers

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

조회 수: 2,049(최근 30일)
I receive the error:
??? Attempt to execute SCRIPT untitled as a function.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2013년 10월 14일
편집: MathWorks Support Team 2018년 2월 12일
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
Walter Roberson
Walter Roberson 2020년 8월 13일
I think you might have accidentally edited the file and added some characters before the "function" keyword

댓글을 달려면 로그인하십시오.

추가 답변(15개)

Lode
Lode 2016년 1월 24일
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
Wong Yen Ann
Wong Yen Ann 2020년 6월 12일
you can just type filename in the matlab console without the run()

댓글을 달려면 로그인하십시오.


Bruce Lin
Bruce Lin 2015년 11월 2일
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
Victory Friday
Victory Friday 2021년 3월 26일
THANK YOU, YOUR ANSWER SOLVED MY PROBLEM

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2015년 5월 4일
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 2020년 5월 14일
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!

MAHESH BOLISETTY
MAHESH BOLISETTY 2019년 11월 11일
can u show the same syntax for perceptron script

Raed Noor
Raed Noor 2019년 11월 12일
편집: Raed Noor 2019년 11월 12일
What if I deleted the file and I don’t have an access to it anymore.

Ivan Dwi Putra
Ivan Dwi Putra 2019년 11월 27일
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 2020년 5월 24일
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
Walter Roberson
Walter Roberson 2020년 6월 13일
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.

댓글을 달려면 로그인하십시오.


Anish Gunjal
Anish Gunjal 2020년 7월 4일

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 2020년 11월 22일
편집: Walter Roberson 2021년 7월 3일
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
Steven Lord
Steven Lord 2020년 11월 22일
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.)

댓글을 달려면 로그인하십시오.


ahsan meo
ahsan meo 2020년 11월 25일
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
Steven Lord
Steven Lord 2021년 1월 4일
In addition to Walter's correction, don't try to call ode45 with @backstepping as its first input from within backstepping.m itself.

댓글을 달려면 로그인하십시오.


Ritu Roy Chowdhury
Ritu Roy Chowdhury 2021년 3월 16일
Hello,
Also make sure that the name of the script doesn't include hyphens (-), and other illegal characters.
Best

madhuri parimi
madhuri parimi 2021년 6월 29일
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

댓글을 달려면 로그인하십시오.


madhuri parimi
madhuri parimi 2021년 6월 30일
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
Walter Roberson
Walter Roberson 2021년 6월 30일
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.

댓글을 달려면 로그인하십시오.


madhuri parimi
madhuri parimi 2021년 7월 3일
편집: Walter Roberson 2021년 7월 3일
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
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 7월 3일
Your MATLAB is corrupt. I recommend that you buy a newer version.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by