Using a function as an input to another

5 visualizaciones (últimos 30 días)
kor vec
kor vec el 22 de Oct. de 2018
Comentada: madhan ravi el 23 de Oct. de 2018
I have a function with two output arguments. I'm trying to pass it as an input to another function where I will need both outputs of the first function.
This is just a minimal example. The two functions:
function [X y] = M(d)
X = d;
[~,Y] = d;
end
function [A B] = N(a)
A = 2*a;
B = 3*a;
end
Basically, I want the following two lines to produce the same output
[a b] =N(3)
M(N(3))
  1 comentario
madhan ravi
madhan ravi el 22 de Oct. de 2018
add comments to your code so that its easier to understand what you are trying to do

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Oct. de 2018
No, when a function has multiple outputs, the only way to capture them all is to use a real function and assign the results to variables. It is not possible in MATLAB to capture multiple outputs of a function inside an expression.
  1 comentario
kor vec
kor vec el 22 de Oct. de 2018
Thanks. I guess I will have to work my way around it.

Iniciar sesión para comentar.

Más respuestas (1)

madhan ravi
madhan ravi el 22 de Oct. de 2018
Editada: madhan ravi el 22 de Oct. de 2018
function [X y] = M(d) %like this not sure what you want to do but a guess
X = d;
[~,Y] = d;
[A B] = @N
function [A B] = N(a)
A = 2*a;
B = 3*a;
end
end
  7 comentarios
Walter Roberson
Walter Roberson el 23 de Oct. de 2018
[A B] = @N
would try to assign a handle to the function N to both A and B, but would fail because the operation of taking a handle to a function does not return multiple outputs.
madhan ravi
madhan ravi el 23 de Oct. de 2018
Oh ok sir thank you for clarifying

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by