Error: Function definition not supported in this context. Create functions in code file.

161 ビュー (過去 30 日間)
Hello,
I was trying to create a simple function on matlab, I already have a file named "AddOne.m", so it should work, however at the first line of code, matlab throws the following error
"Error: Function definition not supported in this context. Create functions in code file.". Any ideas how to sort this out?
Thanks in advance!
matlab pifiando la funcion.png
  1 件のコメント
Stephen23
Stephen23 2020 年 1 月 28 日
In MATLAB functions cannot be defined in the command window.
You appear to be running the entire function in the command window in an attempt to define that function.
You don't need to do that: just define the function in a file, and then call it. That is all you need to do.

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

採用された回答

Deepak Gupta
Deepak Gupta 2020 年 5 月 6 日
Hi Augstin,
In matalb, you need to write your function in a seperate matlab file and then you can call this function either from command window or from a seperate matlab file.
For example, here you can write your matlab function addOne.m in one file. File content can be something like:
%File addOne.m
function finalAnswer = addOne(inputNumber)
finalAnswer = inputNumber+1;
end
Now you can use this function in your other matlab file or in command window. For example, you can create a seperate file to call this function.
% File testaddOne.m
inputNumber = 5;
finalAnswer = addOne(inputNumber);
disp('Thining...');
fprintf('Final Answer: %d \n', finalAnswer);
display('Done');
To avoid confusion, for now keep both files at one place. When you run file testaddOne.m, it will automatically call other file and make use of it.
Cheers.

その他の回答 (5 件)

kazi sharif
kazi sharif 2020 年 9 月 8 日
編集済み: Walter Roberson 2022 年 2 月 22 日
function Divide_Average(a,x0,tol)
clc
if a<0
b=abs(a);
else
b=a;
%fprint(' Neagtive Number is not taken in Square root');
end
x = zeros(1,1);
fprontf('i x(i)\n');
fprintf('---------\n');
x(1)= x0; fprintf('%5.0f\t %15.5f\n',1,x(1));
i=2;
while (1)
x(i) = 0.5*(x(i-1) + a/x(i-1));
fprintf('%5.0f\t %15.5f\n',i,x(i));
RelER = (x(i) - x(i-1))/x(i-1);
if abs(RelER) <= tol, break, end
i = i + 1;
end
fprintf('\n\n Square Root of %10.5f\t is %10.5f\n',a,x(end));
end
function Divide_Average(a,x0,tol)
Error: Function definition not supported in this context. Create functions in code file.
>>

nina lucia lins dias
nina lucia lins dias 2022 年 3 月 13 日
編集済み: Walter Roberson 2023 年 8 月 1 日
I am trying to do this:
function edge_r ( )
im = imread('rice.png');
[row col] = size(im)
for i=1:row-1
im2(i,j) =abs(im(i+1,j+1)) + abs(im(i+1,j));
end
end
figure, imshow(im2);
figure, imshow(im)
however i cannot writing down the whole statement cos it gave me errors
function edge_r()
function edge_r()
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in
code files.
  2 件のコメント
Steven Lord
Steven Lord 2022 年 3 月 13 日
You cannot type the function keyword in the Command Window. Save that function in a file named edge_r.m and then call the function in the Command Window.

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


Emmanuel
Emmanuel 2023 年 8 月 1 日

function predator_simulation() ↑ Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.

  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 1 日
Functions cannot be defined at the command line. You must store that code in predator_simulation.m file.

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


Fathima
Fathima 2024 年 6 月 10 日
function bitDepth = fetchBitDepth(imageData)
Error: Function definitions are not supported in this context. Functions can only be created as local or nested functions in code files.
  1 件のコメント
Stephen23
Stephen23 2024 年 6 月 10 日
編集済み: Stephen23 2024 年 6 月 10 日
Functions cannot be defined at the command line. You must store that code in fetchBitDepth.m file.
Tip for the future: read the thread before posting.

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


lee
lee 2025 年 3 月 7 日
Sorry I'm sure this has been asked here, I am terriblew with matlab. I used to run this code, but now I get the error message: Function definitions are not supported in this context. Functions can only be created as local or nested functions in code
files.
Can someone please help me on how to fix it, code and instruction from original author attached
  2 件のコメント
Stephen23
Stephen23 2025 年 3 月 7 日
編集済み: Stephen23 2025 年 3 月 7 日
"Can someone please help me on how to fix it"
Here is the first line of that Mfile:
calculatorISR('testrange',[1,2,3,4,5,6])function calculatorISR(varargin)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !!!!! DELETE THIS !!!!!
You need to delete everything before the FUNCTION keyword. When you add code before (or after) the function definition then you turn the Mfile into a script and it cannot be called as a function any more.
The first line of the Mfile should be this:
function calculatorISR(varargin)
and nothing else.
lee
lee 2025 年 3 月 7 日
You are an absolute life saver, thank you so much. I'm not a natural programmer haha, and even then tend to use python rather than matlab.
Thank you so much!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by