How can I use an return value from function1 in function2 (different m.files)?
1 回表示 (過去 30 日間)
古いコメントを表示
How can I use an return value from function1 in function2 (different m.files)
1)
function returnval1 = functionname1(input1, input2, input3) ...
2)
function functionname2()
if (returnval1 ~=0) ....
I want to use returnval1 in my 2nd function, but how can I get this value?
I tried it with "returnval1 = functionname1.returnval1;" but that doesn't work... Can you give me a hint or a solution. I guess I'm searching for a function or command... Would be great, if you help me!
0 件のコメント
回答 (1 件)
José-Luis
2012 年 10 月 18 日
編集済み: José-Luis
2012 年 10 月 18 日
You need to pass returnval1 to fun2:
function returnval1 = fun1(input1,input2,input3)
%do your stuff
function fun2(returnval1)
%do your stuff
Then you can use the output of one as the input of the other, e.g. using anonymous functions:
fun = @(input1,input2,input3) fun2(fun1(input1,input2,input3));
And call it (provided fun1 and fun2 are in your path):
fun(input1,input2,input3)
You could also used nested functions (fun1 inside f2). For the sake of completeness, I will mention that you could use globals. However, don't use globals. If you are certain that you need globals, don't use them. If the fate of mankind depends on your use of globals, ask your neighbor if there is a way you can avoid them.
参考
カテゴリ
Help Center および File Exchange で Classification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!