MATLAB: Error: Function definitions are not permitted in this context.

functionMATLAB

I am not able to use function as all in my code, I am confused now that is using because I am suing R2015b version? Do I need to buy the toolbox ?

Best Answer

It is never valid to copy and paste a "function" definition to the command line. It is also not valid to eval() a character vector that contains a "function" definition.
In releases up to R2015a, it was never valid to have a "function" definition inside a script file (a .m file in which the first executable word was not "function" and not "classdef"). In R2015b, it became legal to define functions inside of script files, provided that they were after the script, and provided that every "function" definition ended with a corresponding "end" statement.
Whether in functions or scripts, there are places where it is not valid to have a function definition. For example it is not valid to have a function definition inside a for loop or if statement.
if true
function result = test %invalid function
result = 5;
end
end
Related Question