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

조회 수: 4 (최근 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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by