필터 지우기
필터 지우기

How can I use an return value from function1 in function2 (different m.files)?

조회 수: 1 (최근 30일)
Simon
Simon 2012년 10월 18일
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