Error Message: Function definitions are not permitted in this context

2 views (last 30 days)
Hi I'm new enough to matlab. I am having a problem with running my code and was wondering if anyone can give me advice. Whenever I run it and error message appears saying ' Function definitions are not permitted in this context '. This is the code:
function f=path(mu,lambda,Y0,sigma,N)
dW=sigma*randn(N-1,1);
ARpath=zeros(N,1);
ARpath(1)=Y0;
for i=2:N
ARpath(i)=ARpath(i-1)+mu+lambda*ARpath(i-1)+dW(i-1);
end
f=ARpath;
end

Answers (2)

ES
ES on 5 Jul 2014
You cannot write a function definition on Matlab Command Window.
Open editor and paste your code in a file and save it and run it from the editor or command window.
ALSO: path is a inbuilt function. If you create a function by an inbuilt function name, the inbuilt will be overridden. take care..

Image Analyst
Image Analyst on 5 Jul 2014
Chances are you have a script and function in the same m-file. You can't do that - they all have to be functions. For example in test.m, you cannot do this
f1 = myPathFunction; % Call the function from a script.
function f =m yPathFunction(mu,lambda,Y0,sigma,N)
% Code goes here.
However if test is a function, it's fine:
function test()
f1 = myPathFunction; % Call the function from inside another function.
function f =m yPathFunction(mu,lambda,Y0,sigma,N)
% Code goes here.
Also notice that I did not use the reserved, built-in function "path" for the name of the functions. You should not do that either. Change the name of your function. It's very risky/dangerous to change the definition of built-in functions.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!