How do I call a function within another function?

조회 수: 676 (최근 30일)
Sterling Baird
Sterling Baird 2019년 1월 21일
편집: Yushuo 2022년 7월 5일
How do you call a function within a function like
%filename: calculateA.m
function calculateA(arg1, arg2)
%calculations
function calculateB(arg1)
%calculations
end
%calculations
end
where you're trying to call calculateB(arg1) from say, the command window?
  댓글 수: 1
Stephen23
Stephen23 2019년 1월 22일
편집: Stephen23 2019년 1월 22일
@Sterling Baird: your question shows some confusion. You ask "How do I call a function within another function?", but your example shows functions being defined. Calling a function and defining a function are two totally different things:
  • Defining a function:
function y = myfun(x)
y = sin(x);
end
  • Calling a function:
out = myfun(0.1);
Which of these do you actually want to ask about?

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 2019년 1월 21일
편집: David Goodmanson 2019년 1월 22일
[ MODIFIED to use the terminology 'nested functions']
Hi Sterling,
You can define nested functions within other functions as in the following example.
function z = xsixth(x)
z = xsquare(x).^3;
function a = xsquare(b)
a = b.^2
end
end
Here the nested function xsquare is local to the function xsixth, and calling xsquare from the command line results in an error. If for some reason you need the output of (in this example) xsquare, you can either define it as a separate function and no longer a nested function or do something like the following
function [z y] = xsixth(x)
z = xsquare(x).^3;
y = xsquare(x);
function a = xsquare(b)
a = b.^2
end
end
with the extra output y pulling out the result.
  댓글 수: 2
Sterling Baird
Sterling Baird 2019년 1월 21일
This is what I was looking for. Wasn't sure if it was possible, but that solution makes sense.
Stephen23
Stephen23 2019년 1월 22일
편집: Stephen23 2019년 1월 22일
Note that David Goodmanson shows a nested function:
The most useful feature of nested functions was not mentioned in this answer: nested functions can access variables in the main function's workspace:
function z = xsixth(x)
a = [];
xsquare(x);
z = a.^3;
function xsquare(b)
a = b.^2
end
end
Local functions are not nested within another function, but are written in the same file:
The different function types are explained in the documentation:
The MATLAB documentation does not use the terminology "subfunction".

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jim Riggs
Jim Riggs 2019년 1월 21일
Your question is asking how to CALL a function from within another function, but your sample code is trying to DEFINE a function within another function. This you can't do. You define the functions in separate files:
%filename calculateA.m
function calculateA(arg1, arg2)
%calculations
..
[] = calculateB(arg) % you may call a function within a function simply by referencing it
% file calculateB.m must be in the Matlab path
end
separate file:
%filename calculateB.m
function calculateB(arg1)
% calculations
...
end
  댓글 수: 3
Jim Riggs
Jim Riggs 2019년 1월 22일
It was my understanding that he wanted the function to also be callable from the Matlab command window. Isn't it true that nested functions are limited in scope?
David Goodmanson
David Goodmanson 2019년 1월 23일
Hi Jim, yes they are limited. The the answer I posted is a means of retrieving the output of the nested function to provide an intermediate result, which seems reasonable. However, I had not quite realized the extent to which the OP wanted to both provide an independent input and retrieve the output of the the nested function. Doing both of those by way of extra input and output arguments of the main function does not seem like good programming practice, and at that point it would make more sense to have an independent function like you were talking about initially.

댓글을 달려면 로그인하십시오.


Yushuo
Yushuo 2022년 7월 5일
You can just call directly witin one function, for example
disp(hahaha(5));
function A=hello(x)
A= 5*x;
end
function b=hahaha(tv)
b=hello(tv);
end
result will be 5
  댓글 수: 2
Steven Lord
Steven Lord 2022년 7월 5일
This works because you're writing functions in a script and calling those functions from the script (except it displays 25 instead of 5) but it would not work for the original question. If you put those two functions in a function file and try to call hahaha from the MATLAB prompt, MATLAB will error. Only the main function in a function file (the first one in the file) is directly callable by code outside that file.
That doesn't mean you can't indirectly call that function as long as the main function is willing to help. Consider this file:
function f = example440767
f = @localFunction;
end
function y = localFunction(x)
y = 3*x.^2;
end
If you were to call the main function in example440767.m with an output argument:
f = example440767
f would be a function handle to the local function. Because the main function in example440767 can "see" the local function localFunction inside its file, it can create a function handle that can be used to call that function like this. Note that this call is outside the file example440767.m and so localFunction is not directly callable (in scope) at this point.
>> f(5)
ans =
75
Yushuo
Yushuo 2022년 7월 5일
편집: Yushuo 2022년 7월 5일
Yes, I wrote the functions in one file, if they are in different files then your method is good

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by