0

I have a massive script consisting of many code sections that I run independently of each other. For some of these code sections, there is a lot of repeating code, and so I wanted to define a function that I can call multiple times from a given code section. However, I am either getting the error "Function definitions are not permitted in this context.", or, once the code execution reaches the function call, it says the function is not defined.

So it seems that Matlab (2016b) does not accept functions to be defined within code sections, or I am doing something else that's wrong.

What I tried:

  • define the entire script as a function, named exactly as the name of the containing .m file, and with a corresponding 'end' on the very last line

  • define the function containing my repeating code either at the end of the code section for which it is relevant

  • .. or at the end of the file (before the top-most function's own 'end')

  • .. or at the end of the file (after the top-most function's own 'end')

My code organisation might be criticised, e.g. I might instead use multiple functions in my file, rather than script-style code sections. However, I would like to know whether there is a way to be able to call functions from inside code sections.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
z8080
  • 571
  • 3
  • 7
  • 23
  • If you want to be able to run cells independently, the best way is probably to define the functions in separate files (file name = function name + ".m"). The function file need to be in the working directory or on the MATLAB path. Is there some reason this doesn't work for you? – verbatross Mar 02 '18 at 17:04
  • You're right. I've been trying to keep all code in the same file for convenience, but am prepared to accept this may not be doable :) – z8080 Mar 03 '18 at 10:30

1 Answers1

1

You need to read the following documentation:

Notably, the second contains the relevant information:

Starting in R2016b, another option for storing functions is to include them at the end of a script file.

You say you're using R2016b, so you can define functions within scripts, but they must be at the end of the file. The documentation contains the following example of a valid script containing functions:

x = 3;
y = 2;
z = perm(x,y)

function p = perm(n,r)
    p = fact(n)*fact(n-r);
end

function f = fact(n)
    f = prod(1:n);
end
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • 1
    Function definitions only work if you run the script from the command line, which OP is not doing (unless I misunderstood). – verbatross Mar 02 '18 at 17:01
  • 1
    How else are you running scripts that this method would fail? You can run a script (with or without local functions) from the command line or another script, or by pressing Run / F5 in the editor... – Wolfie Mar 02 '18 at 17:22
  • 1
    I'm guessing OP is using Ctrl-Enter from within the MATLAB IDE to run individual cells. But perhaps I'm misreading – verbatross Mar 02 '18 at 18:15
  • Thanks @Wolfie. I think the cases you mention are not specific to cells, which is what seems to be causing the problem – z8080 Mar 03 '18 at 10:33
  • @verbatross you're right that I've been running my cells by using Ctrl+Enter. I know I can have functions at the end of the script, but it seems to be their call from inside of a cell (itself ran with Ctrl+Enter) that is the source of the incompatibility?... – z8080 Mar 03 '18 at 10:33
  • 1
    In that case verbatross' comment on your question is correct, you simply need to restructure your code. The ctrl+enter is intended for quick running and debugging, not to be the main mechanism you use for running code... separate your functions into different m files, then you can keep the main body to be run as sections if you wish – Wolfie Mar 03 '18 at 17:52