Getting the x from a function file to call inside another function file
1 次查看(过去 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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!