Why do I receive the error "Attempt to execute SCRIPT FILENAME as a function"?
Ältere Kommentare anzeigen
I receive the error:
??? Attempt to execute SCRIPT untitled as a function.
Akzeptierte Antwort
Weitere Antworten (3)
Lode
am 24 Jan. 2016
12 Stimmen
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...
Bruce Lin
am 2 Nov. 2015
7 Stimmen
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.
2 Kommentare
Steven Lord
am 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.
Bruce Lin
am 6 Nov. 2015
Good point, thanks!
Walter Roberson
am 4 Mai 2015
Bearbeitet: MathWorks Support Team
am 30 Dez. 2021
2 Stimmen
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.
Kategorien
Mehr zu Introduction to Installation and Licensing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!