Asked by Agata
on 16 Oct 2011

Hello! I'm extremely new to Matlab, and I'm working on a homework problem, and I keep coming up with an error... I've written my functions, and defined some variables to be plugged into them. I can't even call my functions, because I get the error for writing them.

This all has to be in one m-file so I cannot save the functions in different ones... I'm not sure what to do :(

function [x,y,vx,vy] = trajectory(t,v0,th0,h0,g)

x = v0 .* cos(th0) .* t; y = h0 + (v0 .* sin(th0) .* t) - ((1./2) .* g .* (t.^2)); vx = v0 .* cos(th0); vy = (v0 .* sin(th0)) - (g .* t);

function y = height(t,v0,th0,h0,g)

[x,y,vx,vy] = trajectory(t,v0,th0,h0,g);

%(b) v0 = 20; th0 = 45; h0 = 5; g = 9.81; t = linspace(1,4,400);

y = height(t,v0,th0,h0,g)

Answer by Fangjun Jiang
on 16 Oct 2011

Accepted Answer

You need to understand the difference between M-script and M-function.

If the first line of your .m file is a function definition like you have in the code. This .m file is a M-function.

If the first line of your .m file is not a function definition, but other M command or M code, the .m file is called M-script.

You can not have M-function definition in M-script. That is the error message tries to tell you.

You can save the two functions you have in two .m file and then write a script to call the function.

Please vote here to voice your request. http://www.mathworks.com/matlabcentral/answers/7690-please-vote-if-you-want-to-have-m-function-in-m-script

Answer by Walter Roberson
on 16 Oct 2011

In addition to what Fangjun wrote:

When you are not already executing within a given file, MATLAB can only find the very first function in that file, and that first function name must be the same name as the file.

Therefore, the order of functions in the file should be that the very first one is the "driver" function (the one that sets up everything and calls the other functions to do the work), and the functions that do the internal work should be after that in the file.

If you look at the function order you have coded above, you have coded the internal routine first, and then coded a routine that calls that internal routine. You would, however, not be able to activate that second routine from the MATLAB command line.

So... what you need to do is take the line that start at %(b) through to the end of the file, and move those lines to the beginning of the file, and then you have to insert a "function" line at the very top, naming it appropriately for your assignment conditions. I can see from the code that those lines set things up and then call the internal routines, so those lines should be in the first function.

Log in to comment.

Answer by Kamil Kasic
on 28 Jan 2014

Edited by Walter Roberson
on 28 Jan 2014

What is wrong here?

basic example from Matlab help:

function y = average(x) if ~isvector(x) error('Input must be a vector') end y = sum(x)/length(x); end function y = average(x) | Error: Function definitions are not permitted in this context.

Walter Roberson
on 28 Jan 2014

Alexandria James
on 9 Jan 2016

Thank you! This was much simpler to understand!

Log in to comment.

Answer by Gedion Teklewolde
on 26 Mar 2014

Edited by Gedion Teklewolde
on 26 Mar 2014

Even when it is saved in appropriate name file.m it still fails.

clc clear clc % % Newton-Raphson method % function [x0,err] = newraph(x0) maxit = 100; tol = 1.0e-6; err = 100.0; icount = 0; xold =x0; while (err > tol & icount <= maxit) icount = icount + 1; f = funkeval(xold); df = dfunkeval(xold); xnew = xold - f/df; if (icount > 1) err = abs((xnew - xold)/xnew); end fprintf(1,'icount = %i xold = %e f = %e df = %e xnew = %e err = %e \n',icount, xold, f, df, xnew, err); xold = xnew; end % x0 = xnew; if (icount >= maxit) % you ran out of iterations fprintf(1,'Sorry. You did not converge in %i iterations.\n',maxit); fprintf(1,'The final value of x was %e \n', x0); end function f = funkeval(x) f = x + log(x); function df = dfunkeval(x) df = 1 + 1/x;

Walter Roberson
on 10 Jan 2016

Log in to comment.

Answer by Gireesha Obulaporam
on 28 Jan 2017

I wold like to implement a Genetic Algorithm in MATLAB. So, first I tried to execute the fitness value. I entered the function name called myFitness() which is as shown below:

function y = myFitness(x) It displays me the "Error: Function definitions are not permitted in this context". Please suggest me how to resolve it.

Walter Roberson
on 28 Jan 2017

You can never use "function" at the command line.

If you are using R2016a or earlier then functions can only be defined in a file that starts with function or classdef. In R2016b you can also put functions in a script.

Log in to comment.

Answer by Valeria Martinuzzi
on 5 Jun 2017

Edited by Walter Roberson
on 5 Jun 2017

This function file is giving me an error even though it seems right. It is telling me that "Function definitions are not permited on this context" This is the file:

function [s,flag] = setupSerial(s)

%Initialize the serial port communication between Arduino and MATLAB %The input value is the COMPORT should be changed as per requirement %We ensure that the arduino is also communication with MATLAB at this %time. A predefined code on the Arduino acknowledges this. %If setup is complete then the value of setup is returned as 1 else 0 flag = 1;

s= serial('COM3');

set(s,'DataBits', 8);

set(s,'StopBits', 1);

set(s,'BaudRate', 9600);

set(s,'Parity','none');

fopen(s);

a='b';

while (a~='a')

a=fread(s,1,'uchar');

end

if (a=='a')

disp('serial read');

end

fprintf(s,'%c','a');

mbox = msgbox('Serial Communication setup.'); uiwait(mbox);

fscanf(s,'%u');

end

Help please?

Walter Roberson
on 5 Jun 2017

You need to store that code in a file named setupSerial.m

Log in to comment.

Answer by Nkwentie Musi
on 11 Jul 2017

after defining my function like this Function(Zg,Zt,Zc,Yg,ZT,YT)= LineParameters(Mu,Eo,Rsu,Geom,Ncon,Ns,w) i have this error when executing the program "??? Error: File: testfinal.m Line: 41 Column: 1 Function definitions are not permitted in this context." what was i suppose to do

Walter Roberson
on 11 Jul 2017

Function(Zg,Zt,Zc,Yg,ZT,YT)= LineParameters(Mu,Eo,Rsu,Geom,Ncon,Ns,w)

is not valid syntax for defining a function. You need [] instead of () on the left hand side:

function [Zg, Zt, Zc, Yg, ZT, YT] = LineParameters(Mu, Eo, Rsu, Geom, Ncon, Ns, w)

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Mamoona Yousaf (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/18401-please-help-error-function-definitions-are-not-permitted-in-this-context#comment_393503

Function definitions are not permitted in this context. plz tell me the meaning of this error

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/18401-please-help-error-function-definitions-are-not-permitted-in-this-context#comment_393508

Essentially, the code simplifies down to this:

There is nothing wrong with having that all in the same m-file. You can run it and won't get the error. Basically it runs trajectory, which never calls height at all. If it did, then since height calls trajectory, you could get an infinite loop because it would never stop.

What they

probablydid (but did not show) was to define t,v0,th0,h0,g in the same m-file, and this turned the two-function file (which is allowed) into a script+(2 functions) file which is not allowed. You can't start out an m-file with a script (like defining input arguments) and then follow up with function definitions. You can have a script only, or multiple functions, but not both in the same m-file.## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/18401-please-help-error-function-definitions-are-not-permitted-in-this-context#comment_393547

Image Analyst: scripts can now have functions in them, as of R2016b.

Log in to comment.