Getting the x from a function file to call inside another function file

1 回表示 (過去 30 日間)
Mark Loui
Mark Loui 2021 年 3 月 20 日
回答済み: Jan 2021 年 3 月 20 日
Hi there i need help, i have written a function file lets say
[m,c]=myfunction(x,y) where i defined the x and y inside this function file,
in a seperate function file, lets says [y]=myfunction2(m,c)
How can i defined the x and y i have called/defined in the previous function file
Note, i need to call this x again back to get the length for the n to be run within the function file myfunction2
  1 件のコメント
Jan
Jan 2021 年 3 月 20 日
"written a function file lets say [m,c]=myfunction(x,y) where i defined the x and y inside this function file" - this is not clear yet. It seems like x and y are inputs of this function and not defined inside. Posting some working code is always the best idea.

サインインしてコメントする。

採用された回答

Jan
Jan 2021 年 3 月 20 日
The reliable way to provide the values of variables defined inside a function is to export them as output arguments. If they are need inside other functions, forward them as input arguments:
function main
c = rand;
[a, x, y] = fcn1(c)
fcn2(a, x, y)
end
function [a, x, y] = fcn1(c)
x = 1 - c;
y = c^2;
a = x - y;
end
function [b] = fcn2(a, x, y)
a
x
y
end
You could use nested functions also, but this increases the complexity of the code also. GLOBAL variables can share variables between functions also, but they are a shot in your knee and a bad programming practize, because they are very hard to debug.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by