External function calling inside another function

조회 수: 17 (최근 30일)
KUMAR SOURAV
KUMAR SOURAV 2013년 10월 9일
댓글: Steven Lord 2021년 7월 22일
Can an external function created be called inside another function.Please explain with an example. Thanks.

채택된 답변

ES
ES 2013년 10월 9일
편집: ES 2013년 10월 9일
Definitely yes, provided the path of the external function (m file/m script) exists in the matlab path.
.m
function script1(arg1, arg2, arg3)
....
....
x=script2(arg4,arg5);
script2.m
function y=script2(arg6, arg7, arg8)
y=arg6 + arg7...

추가 답변 (2개)

sixwwwwww
sixwwwwww 2013년 10월 9일
Here is a simple example of calling one function within another function:
% Function 1 calling function 2
function output1 = fun1(input1, input2)
value_fun2 = fun2(input1, input2);
output1 = value_fun2 * input1 * input2;
end
% Defining function 2
function output2 = fun2(in1, in2)
output2 = in1 + in2;
end
You can save both function in the same folder then it will work fine
  댓글 수: 2
Urifhe Mathagwa
Urifhe Mathagwa 2021년 7월 22일
What if fun2 has to accept morethan 2 input
Steven Lord
Steven Lord 2021년 7월 22일
Then you define it to accept more than 2 inputs and you have fun1 call it with more than 2 inputs. Nothing about this technique limits it to only working with two inputs.
out1 = fun1(2, 3, 4)
out1 = 852
% Function 1 calling function 2
function output1 = fun1(input1, input2, input3)
value_fun2 = fun2(input1, input2, input3, 42);
output1 = value_fun2 * input1 * input2;
end
% Defining function 2
function output2 = fun2(in1, in2, in3, in4)
output2 = in1.^in3 + in2.*in4;
end

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


Adam Wide´n
Adam Wide´n 2021년 1월 28일
Hi!
How can I write a cylinder volume as a external fuction, there radius r , height h are fuction arguments?
best wishes
Adam

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by