MATLAB Answers

2

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

Asked by MathWorks Support Team on 14 Oct 2013
Latest activity Commented on by Walter Roberson
on 12 Nov 2019 at 2:35
I receive the error:
??? Attempt to execute SCRIPT untitled as a function.

6 Answers

Answer by MathWorks Support Team on 14 Oct 2013
Edited by MathWorks Support Team on 12 Feb 2018
 Accepted Answer

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.

  6 Comments

You saved my @$$ bro!! I appreciate it..
"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"
This was my problem, which I encountered in a code from a git repository, where the authors put a "dummy" .m file for documentation purposes of an .mex file.
You really saved my day, as this could have caused me to spend countless hours trying to figure out what's wrong!

Sign in to comment.


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

  1 Comment

It was very useful to me. thank you very much!

Sign in to comment.


Answer by 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

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.
I faced the same error and I fixeded it by following your answer. Thank you so much.

Sign in to comment.


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


Answer by MAHESH BOLISETTY on 11 Nov 2019 at 9:08

can u show the same syntax for perceptron script

  1 Comment

You should probably create a new Question for this.

Sign in to comment.


Answer by Raed Noor
on 12 Nov 2019 at 1:28
Edited by Raed Noor
on 12 Nov 2019 at 1:29

What if I deleted the file and I don’t have an access to it anymore.

  1 Comment

I think you will need to expand your question.

Sign in to comment.