Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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!

Respuestas (1)

José-Luis
José-Luis el 18 de Oct. de 2012
Editada: José-Luis el 18 de Oct. de 2012
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 comentario
Simon
Simon el 18 de Oct. de 2012
Editada: Simon el 18 de Oct. de 2012
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'."

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by