Error: Function definitions are not permitted in this context.

52 ビュー (過去 30 日間)
Nabin SUNAM
Nabin SUNAM 2015 年 1 月 22 日
編集済み: Walter Roberson 2021 年 5 月 17 日
This is my function
function [ area ] = calcarea(rad )
area = pi*rad.^2
end
When I try to run it, following message shows up, "The selected section cannot be evaluated because it contains an invalid statement
Also, the command window says "Error: Function definitions are not permitted in this context"
What am I doing wrong???
  1 件のコメント
Nabin SUNAM
Nabin SUNAM 2015 年 1 月 23 日
It worked after I saved the file with the same filename as my function, i.e my filename was not calcarea.m before, once I renamed the filename as calcarea.m, it started functioning. I had to call the function in the command window though.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2015 年 1 月 23 日
You probably have that function below a script in your m-file. You can't mix a script and a function in the same file. You can have two functions though. So if your file is called test.m, you could have all this in the single file:
function test()
area = calcarea(10)
end
function area = calcarea(rad)
area = pi*rad.^2;
end
Or you could have them in two separate files:
A script in test.m:
area = calcarea(10)
A function in calcarea.m:
function area = calcarea(rad)
area = pi*rad.^2;
end
  5 件のコメント
Image Analyst
Image Analyst 2018 年 3 月 6 日
Attach your m-file(s) in a new question (not here in Nabin's question).
Walter Roberson
Walter Roberson 2018 年 3 月 6 日
Sanjiv Kumar what happened when you followed the instructions I gave in your Question to install the contents of the .zip I provided for you?

サインインしてコメントする。

その他の回答 (5 件)

Star Strider
Star Strider 2015 年 1 月 22 日
You have to put functions such as yours in their own separate .m-files. In your situation, you would save it as: calcarea.m, and to run it from your main script, you would call it as:
area = calcarea(rad)
  2 件のコメント
Nabin SUNAM
Nabin SUNAM 2015 年 1 月 23 日
Yes, my problem was I saved the file with different filename. It started working when I renamed the file. Thanks for the quick reply.
Star Strider
Star Strider 2015 年 1 月 23 日
My pleasure!

サインインしてコメントする。


bahadir safak
bahadir safak 2016 年 12 月 17 日
編集済み: Walter Roberson 2018 年 3 月 6 日
sigma=1;
thresh=1000;
disp=1;
radius=1
im=imread('11.jpg');
function [cim, r, c] = harris(im, sigma, thresh, radius, disp)
error(nargchk(2,5,nargin));
dx = [-1 0 1; -1 0 1; -1 0 1]; % Derivative masks
dy = dx';
Ix = conv2(im, dx, 'same'); % Image derivatives
Iy = conv2(im, dy, 'same');
??? Error: File: harris1.m Line: 45 Column: 1
Function definitions are not permitted in this context.
>>
???
  7 件のコメント
Stephen23
Stephen23 2017 年 5 月 4 日
編集済み: Stephen23 2017 年 5 月 4 日
@Mohammedashraf Shaikh: your code is badly aligned, and this makes hiding the errors easy. Once I correctly aligned your code and put each operation on its own line, then the (first) mistake is obvious, and occurs in the first five lines of code:
function[d] = hcompare_EMD(h1,h2)
d = sum(abs(cumsum(h1) - cumsum(h2)));
end
img = imread('C:\Users\Hp\Desktop\col\001.jpg');
function[h] = histImage(img)
...
You call img = ... outside of any function.
It is also possible that your first function definition occurs after some lines of code in a script. Depending on MATLAB version, this will also cause an error. See the other answers for more info on this.
Summary: Learn to align your code consistently (use the MATLAB defaults), and do not put multiple operators onto one line (especially the end for functions!). You can automatically align the code in the MATLAB Editor: select all code, then press ctrl+i.
Image Analyst
Image Analyst 2017 年 5 月 4 日
And learn to format your posts by watching this tutorial Click here

サインインしてコメントする。


Rwigema james
Rwigema james 2017 年 7 月 7 日
編集済み: Image Analyst 2017 年 7 月 7 日
function Iout = readAndPreprocessImage(filename)
I=imread(filename);
if ismatrix(I)
I = cat(3,I,I,I);
end
Iout = imresize(I,[227 227]);
end
Why am I getting this error?????
Function definitions are not permitted in this context.
  2 件のコメント
Image Analyst
Image Analyst 2017 年 7 月 7 日
You're putting the function definition in a place where it does not belong, such as below a script in release R2016a and earlier, or elsewhere.
Abbas Khreis
Abbas Khreis 2021 年 5 月 16 日
i have 2016a version and the error Function definitions are not permitted in this con
plz help me

サインインしてコメントする。


Elena MEZZAPESA
Elena MEZZAPESA 2017 年 11 月 16 日
Hello, Did you solve the issues to this problem in the end? As of 2017 I still get this error ?! I am trying to understand if it is because I am using a trial version of the software. Best regards,
Elena
  1 件のコメント
Image Analyst
Image Analyst 2017 年 11 月 16 日
It was masked as being solved. The trial version is no different than a full version other than being time-limited to 30 days. We'd have to see your code to figure out what's going wrong with your program.

サインインしてコメントする。


Frieder Wittmann
Frieder Wittmann 2018 年 5 月 27 日
This is one of the main disadvantages of MATLAB notebooks over Jupyter (python) notebooks. If I can't define functions in the middle of a script, the notebook becomes hard to read.
  6 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 17 日
MATLAB is not Jupityr.
Frieder Wittmann
Frieder Wittmann 2021 年 5 月 17 日
編集済み: Frieder Wittmann 2021 年 5 月 17 日
It isn't, it's one of the most common competitors.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by