Error Message: Function definitions are not permitted in this context

4 ビュー (過去 30 日間)
Cian
Cian 2014 年 7 月 5 日
回答済み: Image Analyst 2014 年 7 月 5 日
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

回答 (2 件)

ES
ES 2014 年 7 月 5 日
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 2014 年 7 月 5 日
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.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by