How I can return output from function 'A' for using them as input for another function 'B'?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aknur
el 2 de Feb. de 2023
Hi! Kindly ask to help with return variable from function
I want to return data of 'I' from function 'A' and use it as an input for another function 'B'. Appreciate any help or useful documnetation. I have tried handle function, but failed. I suppose handle is for defined functions in MAtlab such as sin, cos, etc
function(I, j0) = calculation(x,y,z,j) %function 'A'
I = x + y + z;
R = I*j;
end
Then I am trying to return 'I' from function 'calculation' and use it as input for 'row' function
function( A,B,C ) = row(I, j0) %function 'B'
%I is point with coordinates
A= ...;
B = ....;
C= ...;
end
6 comentarios
Stephen23
el 2 de Feb. de 2023
Can I ask function hadle should be written in that existing function inside. Or it should be written such as an another function Or I write function handle in the beginning of another fucntion and then call haninsdle."
A function handle can be defined anywhere where that function is within scope.
"Here is my code and I want to return 'I - intersection point' instead of X0, Y0, Z0 to calculate next ray.. And it becomes very difficult for me to understand how to return I and where should write handle."
It is very easy to return I, because it is already defined as the sixth output argument of that function:
function [X, Y, Z, Theta, Phi, I, Rr] = reflection2(X0, Y0, Z0, Theta0, Phi0, K)
% ^ the 6th output is what you want
So you can obtain that output by simply calling the function (or its handle) with whatever outputs you want:
[~,~,~,~,~,I_out] = reflection2
% ^^^^^ call the function with any outputs that you require
How to call functions with output arguments is explained in the introductory tutorials:
It is still unclear how function handles are relevant to this task.
Respuesta aceptada
Stephen23
el 2 de Feb. de 2023
Editada: Stephen23
el 2 de Feb. de 2023
How to call function with output arguments is explained in the introductory tutorials:
How to define function output arguments is explained in the FUNCTION() documentation:
I had to change your parentheses for the correct square brackets and define one of the output arguments:
x_in = 1;
y_in = 2;
z_in = 3;
j_in = 4;
[I_out,j0_out] = calculation(x_in,y_in,z_in,j_in)
[A_out,B_out,C_out] = row(I_out, j0_out)
function [I,j0] = calculation(x,y,z,j)
I = x + y + z;
j0 = 0; % you need to define all output arguments.
% R = I*j; % completely unused
end
function [A,B,C] = row(I, j0) % these input are unused.
A = 1;
B = 2;
C = 3;
end
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Calculus 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!