How I can return output from function 'A' for using them as input for another function 'B'?

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

"I have tried handle function, but failed."
I don't see how a function handle would help return output arguments from a called function.
"I suppose handle is for defined functions in MAtlab such as sin, cos, etc"
You can define a function handle for any function. This is shown in the documentation:
Hi @Stephen23 thank you for your time
According to this documentation I can write variable which will be equal to output of function, right?
f = @computeSquare;
a = 4;
b = f(a)
"According to this documentation I can write variable which will be equal to output of function, right?"
I don't know what that means. According to the documentation you can define a function handle to an existing function, you can then call that function handle. It is unclear what you want to explain by copying part of the example from that page.
@Stephen23 thank you, now I understand. 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.
Here is my code and I want to return 'I - intersection point' instead of X0, Y0, Z0 to calculate next ray
X(i) = I(1);
Y(i) = I(2);
Z(i) = I(3);
And it becomes very difficult for me to understand how to return I and where should write handle. Could you please look at my code and give an advice how 'I should change it
Thank you in advance,
X0=1.5
Y0= 1.5
Z0 =3.0
Theta0 =30
Phi0 =90
K = 2 number of rays
this code works only for one ray
function [X, Y, Z, Theta, Phi, I, Rr] = reflection2(X0, Y0, Z0, Theta0, Phi0, K)
X = [X0 zeros(1,K)];
Y = [Y0 zeros(1,K)];
Z = [Z0 zeros(1,K)];
Theta = [Theta0 zeros(1,K)];
Phi = [Phi0 zeros(1,K)];
end
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.
Hello, @Stephen23 thank you. Now I understand. Your help was very valuable, and thank you for sharing useful documentation

Sign in to comment.

 Accepted Answer

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)
I_out = 6
j0_out = 0
[A_out,B_out,C_out] = row(I_out, j0_out)
A_out = 1
B_out = 2
C_out = 3
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 Comment

Dear @Stephen23 kindly ask why this
'(I, j0) % these input are unused.'
Because I want to use I,j0 to calculate 'row' function.
Real formulas and data are different, they are longer, so I decide to write example. Sorry if I confused you.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 2 Feb 2023

Edited:

on 5 Feb 2023

Community Treasure Hunt

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

Start Hunting!