How I can return output from function 'A' for using them as input for another function 'B'?
4 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 2 Feb 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.
Accepted Answer
Stephen23
on 2 Feb 2023
Edited: Stephen23
on 2 Feb 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
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!