简体   繁体   中英

How get an output from a function in another function?

I have a function Matrices that gives me Along . I want Along from first function to pass another function MatricesElse as an argument. This code has error, why?

function theOutput = Matrices()
    theOutput = Along;


    function MatricesElse(Along)
    disp(theInput);       % // Will print value of Along returned from first function,

syms teta q u w Se Sth v p r o Y SR SA s

% // Aircraft Equations of Longitudinal Motion
Ilong=[1 0 0 0;0 1 0 0; 0 1 0 0;0 0 0 1];
Xlong=[teta;q;u;w];
Ulong=[Se;Sth];
Xlong=(inv(s*Ilong-Along))*Blong*Ulong;
teta=Xlong(1,1)
q=Xlong(2,1)
u=Xlong(2,1)
w=Xlong(3,1)

% // Aircraft Equations of Lateral Motion
Ilat=[1 0 0 0 0;0 1 0 0 0;0 0 1 0 0;0 0 0 1 0;0 0 0 0 1];
Xlat=[v;p;r;o;Y];
Ulat=[SR;SA];
Xlat=(inv(s*I-Alat))*Blat*Ulong;
v=Xlat(1,1)
p=Xlat(2,1)
r=Xlat(3,1)
o=Xlat(4,1)
Y=Xlat(5,1)

end

Short explanation: you are trying to call a private variable of function Matrices() outside of this function, that's why it doesn't work.

This code should work. Note that the name CalcMatrix can differ from theOutput and Along , but all of these variables have the same content:

function main

CalcMatrix = Matrices()
MatricesElse(CalcMatrix)


function theOutput = Matrices()
theOutput = Along;


function MatricesElse(Along)
disp(theInput);       % // Will print value of Along returned from first function,

syms teta q u w Se Sth v p r o Y SR SA s

% // Aircraft Equations of Longitudinal Motion
Ilong=[1 0 0 0;0 1 0 0; 0 1 0 0;0 0 0 1];
Xlong=[teta;q;u;w];
Ulong=[Se;Sth];
Xlong=(inv(s*Ilong-Along))*Blong*Ulong;
teta=Xlong(1,1)
q=Xlong(2,1)
u=Xlong(2,1)
w=Xlong(3,1)

% // Aircraft Equations of Lateral Motion
Ilat=[1 0 0 0 0;0 1 0 0 0;0 0 1 0 0;0 0 0 1 0;0 0 0 0 1];
Xlat=[v;p;r;o;Y];
Ulat=[SR;SA];
Xlat=(inv(s*I-Alat))*Blat*Ulong;
v=Xlat(1,1)
p=Xlat(2,1)
r=Xlat(3,1)
o=Xlat(4,1)
Y=Xlat(5,1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM