Accessing data variables from another function

3 visualizaciones (últimos 30 días)
Billy Worley
Billy Worley el 17 de Nov. de 2019
Comentada: Billy Worley el 17 de Nov. de 2019
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.

Respuesta aceptada

Erivelton Gualter
Erivelton Gualter el 17 de Nov. de 2019
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 comentario
Billy Worley
Billy Worley el 17 de Nov. de 2019
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!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by