Solve integral in nested function

hello there, i am trying to build something like this:
if true
function x = first
x = 5*y
function y = nested
I = @(x) x^2
y = integral(I,0,1)
end
end
end
but there occurs an error stating: 'Error: Function definitions are not permitted in this context.'
Does anyone know an alternative or sees the misstake i made?
thanks!

回答 (1 件)

Meet
Meet 2024 年 7 月 26 日

0 投票

Hi Payjay,
According to the MATLAB R2024a documentation, you cannot define a nested function inside any of the control statements, such as if, elseif, else, switch, case, for, while, or try/catch.
What you can do is define the nested function outside the if control statement and then call the function inside this control statement. For example:
function x = first
% Your code logic
function y = nested
% Your code logic
end
end
if true
result = first
end
For more information on the usage of nested function, you can refer to the following documentation: Nested Functions

カテゴリ

ヘルプ センター および File ExchangeGraphics Objects についてさらに検索

質問済み:

2017 年 3 月 6 日

回答済み:

2024 年 7 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by