How do i call a function inside another function?
142 ビュー (過去 30 日間)
古いコメントを表示
i wrote 2 functions separately. in one of these functions, i need to call the other function inside it? how do i do that? Thanks :)
0 件のコメント
回答 (2 件)
KSSV
2016 年 9 月 3 日
I am giving an example here. The below first function calls a seconds function to calculate the sum of three numbers.
function K = firstfunction(a,b,c)
L = secondfunction(b,c) ;
K = a+L ;
This is the second function which calculates sum of two numbers.
function L = secondfunction(b,c)
L = b+c ;
Call the first function in Main file/ matlab work space:
s = firstfunction(1,2,3) ;
3 件のコメント
Stephen23
2018 年 12 月 18 日
編集済み: Stephen23
2018 年 12 月 18 日
"My question is will the variable L be local or global to the calling function?"
L is not declared as global. It exists
- in the workspace where it is defined, and
- where it is passed as an output argument.
What do you mean by "the calling function" ?
Jan
2018 年 12 月 18 日
@Soumen Kuma Mondal: Try it. It is very cheap to run the shown code and to use the debugger to step throught the code line by line. Then you can see in the WorkspaceBrowser, which variables are existing.
You need the command global to make a variable global. Another method to share data is to created a nested function:
function K = firstfunction(a,b,c)
L = secondfunction(b,c) ;
K = a+L ;
function L = secondfunction(b,c)
L = b+c ;
disp(K)
end
end
Now secondFunction is visible inside the firstFunction only and e.g. K is existing inside secondFunction.
KSSV
2016 年 9 月 3 日
simple....write the function (output = functionname(inputs)) inside the function you want...
参考
カテゴリ
Help Center および File Exchange で Scope Variables and Generate Names についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!