Getting the x from a function file to call inside another function file
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!