Accessing data variables from another function

조회 수: 16 (최근 30일)
Billy Worley
Billy Worley 2019년 11월 17일
댓글: Billy Worley 2019년 11월 17일
I have a function, F1, that will perform a certain amount of calculations, with the results as variables Q and R. The input for F1 is A, an n x n matrix. I need to write another function, F2, that calls F1 and uses Q and R to calculate a new output, A2. This A2 needs to be run back through F1 to calculate a new Q and R, which will then be run through F2 again, to calculate A3, and so on.
My logic behind this is that I can create a for-loop that tags into F1 each iteration, and updates A2 to be A each time. I'm thinking this is possible, but not sure if it is.
Anyhow, the problem I am having is acessing those variables from F1. For example (using a toned down version, I have many more variables in mine):
1st function:
function [Q,R] = F1(A)
Q = 2*A
R = 3*A
end
-------------------------------------------------
2nd function:
function d = F2(A)
fun = @F1
--------------------------------------------
command window input:
F2([1,1,1 ; 2 2 2 ; 3 3 3]
In order for my process to work, my F2 needs to make calculations based on the variables from F1. How can I access those variables, or have those variables input into my workspace, before the function ends? It has to be in this format, a function F1 and function F2.

채택된 답변

Erivelton Gualter
Erivelton Gualter 2019년 11월 17일
I did not get the final goal, but I fixed your code.
F2([1,1,1 ; 2 2 2 ; 3 3 3])
function [Q,R] = F1(A)
Q = 2*A
R = 3*A
end
function d = F2(A)
[Q,R] = F1(A);
d = [];
end
  댓글 수: 1
Billy Worley
Billy Worley 2019년 11월 17일
I realized that my F1 was not saved in the same directory as F2, as F1 was given to me. Still learning Matlab and just noticed that was one of my major problems. Thanks for your input!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by