How can I put 2 functios together in just one function?
1 次查看(过去 30 天)
显示 更早的评论
Hi there! I have created 2 functions :
function [tk]= time(t,toe)
ti=t-toe;
if ti>302400;
tk= ti-604800;
elseif tk<-302400;
tk = ti+604800;
end
end
and the second function :
function [mk] = meanan(mo,a,u,n,tk)
mk = mo +((sqrt(u)/sqrt(a^3))+n)*tk
end
Now my question is : Can I put this 2 functions in just one function in MATLAB? I've been looking on internet but what I found is NO, it is not possible. Is it right? I cannot put 2,3 or maybe more functions in just one? Thank you!
0 个评论
采纳的回答
Walter Roberson
2018-3-28
function mk = meanantime(mo, a, u, n, t, toe)
ti=t-toe;
if ti>302400;
tk= ti-604800;
elseif tk<-302400;
tk = ti+604800;
end
mk = mo +((sqrt(u)/sqrt(a^3))+n)*tk;
But what I suspect you are asking is whether you can put multiple functions into the same .m file and still be able to call each one from outside of the .m file.
The answer to that is "Not easily". Someone did, however, point out the other day that it is possible to create static class methods that can be invoked by name from outside the .m file. This would require changing the combined .m file into a classdef with appropriate structuring.
0 个评论
更多回答(3 个)
Birdman
2018-3-28
Usage of nested functions should help you here:
function tk= time(t,toe,mo,a,u,n)
ti=t-toe;
if ti>302400
tk=ti-604800;
elseif ti<-302400
tk = ti+604800;
end
tk=@meanan;
function [mk] = meanan(x)
mk = mo +((sqrt(u)/sqrt(a^3))+n)*x;
end
end
Since meanan function uses variables which are inputs of time function, it would be wiser to use them nested. Then from command line, call them as follows:
tk=time(1,2,3,4,5,6) %creates a function handle and pass all inputs to meanan function
mk=tk(4)
Inputs are randomly selected. Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!