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

3 ビュー (過去 30 日間)
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 件のコメント
Stephen23
Stephen23 2023 年 2 月 2 日
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.
Aknur
Aknur 2023 年 2 月 3 日
Hello, @Stephen23 thank you. Now I understand. Your help was very valuable, and thank you for sharing useful documentation

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2023 年 2 月 2 日
編集済み: Stephen23 2023 年 2 月 2 日
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 件のコメント
Aknur
Aknur 2023 年 2 月 2 日
編集済み: Aknur 2023 年 2 月 2 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by