How can I put 2 functios together in just one function?

1 view (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2018
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.

More Answers (3)

Birdman
Birdman on 28 Mar 2018
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.

gblmtc
gblmtc on 28 Mar 2018
Thanks guys for your help!
What I acctualy need to do is to do the calculations from the pics I’ve attached.
I did a function for each formula andnow I was thinking to put all togheter to can just call a big function and to get the final result.
I’ve checked FUNCTIONS on Mathworks but couldn’t find a creal answer.
Thanks again!
  4 Comments
gblmtc
gblmtc on 28 Mar 2018
Thanks for your help!
I'm new here and MATLAB drives me crazy! :)))
Yes Birdman, your solution works!
I'll try it with the script to see how it works but I check the nested functions, too.
Again thanks!
Birdman
Birdman on 28 Mar 2018
You are welcome! We are here for further questions.

Sign in to comment.


gblmtc
gblmtc on 28 Mar 2018
Edited: Birdman on 28 Mar 2018
function all_one
function [tk]= time(t,toe)
ti=t-toe;
if ti>302400;
tk= ti-604800;
elseif tk<-302400;
tk = ti+604800;
end
end
function [mk] = meanan(mo,a,u,n,tk)
mk = mo +((sqrt(u)/sqrt(a^3))+n)*tk
end
function [ek] = ecc_an (mk, e)
ek= mk/(1-e)
end
function [vk] = true_an(ek, e)
vk = atan((sqrt(1-e^2)*sin(ek))/(cos(ek)-e)
end
function [uk] = arg_lat(o,vk,cuc,cus)
uk = o + vk + cuc*cos(o+vk) + cus*sin(o+vk)
end
function [rk] = rad_dist(a,e,ek,crc,o,vk,crs)
rk= a(1-e*cos(ek))+crc*cos(2)*(o+vk)+crs*sin(2)*(o+vk)
end
function [ik] = incl(io,i,tk,cic,o,vk,cis)
ik = io+i*tk+cic*cos(2)*(o+vk)+cis*sin(2)*(o+vk)
end
end
So I've created a script and put there all the functions I've created before. I gave values to inputs and now I just write in the command window " all_one" ?
What's the correct way for calling the big function?
Thanks again!

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!