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

1 回表示 (過去 30 日間)
gblmtc 2018 年 3 月 28 日

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!

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

### 採用された回答

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.

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

### その他の回答 (3 件)

Birdman 2018 年 3 月 28 日
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 件のコメント表示 -1 件の古いコメント非表示 -1 件の古いコメント

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

gblmtc 2018 年 3 月 28 日
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 件のコメント表示 3 件の古いコメント非表示 3 件の古いコメント
Birdman 2018 年 3 月 28 日
You are welcome! We are here for further questions.

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

gblmtc 2018 年 3 月 28 日

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
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!

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

### カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

### Community Treasure Hunt

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

Start Hunting!