Error Message: Function definitions are not permitted in this context

2 views (last 30 days)
I still new for this software so I'm not sure this error is a simple or complex error. Hope someone could give me a simple answer. Any reply would be highly appreciated.

Answers (1)

Geoff Hayes
Geoff Hayes on 18 Oct 2015
Edited: Geoff Hayes on 18 Oct 2015
Bill - this error message typically indicates that you have an m file that begins with some code outside of a function definition, and then you define a function somewhere after that code. For example, if I save the following code to an m file, I would observe the same error when I try to evaluate the code.
t = 1;
for k=14;
t = t + 1;
end
function incorrect();
From your screen capture, you show that you have defined the function Tank at line 17 of your file and so you must have done something similar to above. To fix this, you can do one of two things: save the function Tank to its own m file named Tank.m, or nest this function within a main function for the file (with the same name as the m file). For example, if your code is saved to a file names main.m you could do
function main()
% some code
% now nest Tank
function Tank()
% etc.
See nested functions for more details.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!