簡體   English   中英

如何從另一個函數中的一個函數獲得輸出?

[英]How get an output from a function in another function?

我有一個函數Matrices ,讓我Along 我想Along從第一功能通過另一個函數MatricesElse作為參數。 這段代碼有錯誤,為什么?

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

簡短說明:您正在嘗試在此函數之外調用函數Matrices()的私有變量,這就是為什么它不起作用的原因。

此代碼應該起作用。 需要注意的是名字CalcMatrix可以從不同theOutputAlong ,但所有這些變量都具有相同的內容:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM