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!

回答(1 个)

José-Luis
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.
  1 个评论
Simon
Simon 2012-10-18
编辑:Simon 2012-10-18
Hi,
thanks! But it still doesn't work.
function returnval1 = fun1(input1,input2,input3)
%do your stuff
function fun2(returnval1)
%do your stuff
I did this, like you wrote. But when I write
function returnval1 = fun1(input1,input2,input3)
%do your stuff
function fun2(returnval1)
% There I calculated some quotients x and y. I only want to take values
%with a quotient lying in a certain range (that works)
% additionally i take these values with quotient of a certain range and want to exclude values with a returnval1=0. At the end I want to calculate the mean of the rest values, which haven't been excluded.
if ((returnval1~=0))
mean(x)
mean(y)
disp(x)
disp (y)
EDIT: function1 is in a different m.file, not in the same like function2
EDIT2: ??? Input argument "returnval1" is undefined. EDIT3: If I define returnval1 with the definition of the returnval1 in function1, Matlab says "??? Undefined function or variable 'variable of returnval1 definition'."

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by