How can I use an return value from function1 in function2 (different m.files)?
2 次查看(过去 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!