5.4.1: Function Names and MATLAB Keywords
- Page ID
- 84402
User defined MATLAB function names follow the same rules as variable names:
The function name must start with a letter.
It may have numbers and/or an underscore "_"
It cannot have a space, a hyphen, parentheses, a period, nor another symbol.
The function filename and the name on the function declaration line (a.k.a. signature) inside the file should be the same. See this link: https://www.mathworks.com/help/matlab/ref/function.html
Do not give a user-defined function the same name as a built-in function. That causes a "name collision". You can check the name like this:
which sum % MATLAB will tell you that this is a built-in function and will display the path to the library which contains this function.
which my_super_function % MATLAB will tell you that this function is "not found", so it is OK to use as a function name.
If you give your function the name of a built-in function, you can undo this by renaming your function, then enter this command:
clear functions
MATLAB has keywords that are reserved for its own use.
You cannot use these words as names of variables or functions.
Entering "iskeyword" gives a list of MATLAB's keywords in alphabetical order:
'break'
'case'
'catch'
'classdef'
'continue'
'else'
'elseif'
'end'
'for'
'function'
'global'
'if'
'otherwise'
'parfor'
'persistent'
'return'
'spmd'
'switch'
'try'
'while'
This video gives a clear explanation of defining a MATLAB function:
https://www.youtube.com/watch?v=qo3AtBoyBdM
Function M-Files in MATLAB, RobertTalbertPhD