subfunction in Matlab or many functions in one edite file

1 view (last 30 days)
hey
i want to write two functions in one file so the output of the first code will be later use as input in the second one to get the final answer ,the output in the second code
the final answer that i want is R_praezession , first i wrote a function newcomb to get the astronomy angles " zeta,z, teta" then i thougt i can use those in praezession's function as input ,but when i saved the file with name praezession.m and i want to run the both:
[zeta,z,teta]= newcomb(T1,T2);
R_praezession= praezession(zeta,z,teta);
i became this msg " Unrecognized function or variable 'newcomb'." so how can i write two function then i can call the output of the first one as input in the second one ?
note: T1,T2 are already calculated
my code
function R_praezession = praezession (zeta,z,teta)
function [zeta,z,teta]= newcomb(T1,T2)
zeta = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (0.30188 - 0.000344 * T1)...
*T2^2 + 0.017998 * T2^3) / 648000 * pi;
z = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (1.09468 + 0.000066 * T1) * T2^2 + 0.018203 * T2^3) / 648000 * pi;
teta = ((2004.3109 - 0.85330 * T1 - 0.000217 * T1^2) * T2 + (-0.42665 - 0.000217 * T1) * T2^2 - 0.041833 * T2^3) / 648000 * pi;
end
Rz=[cos(-z) sin(-z) 0; -sin(-z) cos(-z) 0; 0 0 1];
Ry=[cos(teta) 0 -sin(teta); 0 1 0; sin(teta) 0 cos(teta)];
Rx_zeta=[cos(-zeta) sin(-zeta) 0; -sin(-zeta) cos(-zeta) 0; 0 0 1];
R_praezession = Rz*Ry*Rx_zeta;
end

Answers (2)

Walter Roberson
Walter Roberson on 14 Nov 2020
You have a few possibilities:
  1. Use three files, one for your script that calls both functions, and one each for the two functions
  2. Use one script file that has the code that calls both functions, and then defines each of the functions ( not nested)
  3. Use a classdef with static methods. Once the class name has been invoked as a function, then its static methods get imported into that workspace
  4. Write a "switchyard" function that has an interface function at the top, and also defines the two functions, and use a parameter to the switchyard to tell it which of the two functions to call on your behalf. For example, [zeta,z,teta]=astro('newcomb',T1,T2); R_praezession=astro('praezession',zeta,z,teta);
  5. Write a function file that includes the two target functions and which returns handles to the fuctions; invoke the factory and get back the function handles and invoke them. For example afuns=astrofuns(); [zeta,z,teta]=afuns.newcomb(T1,T2); R_praezession=afuns.praezession(zeta,z,teta);
  6. Write a class that defines some kind of astro object, and which has methods to do the work. For example, aobj=astro(T1,T2); R_praezession = praezession(aobj); with the lack of method for newcomb being because hypothetically the astroclass has taken T1 and T2 and remembered them and internally calculates zeta,z,teta from them as needed.
  2 Comments
Basem Nouh
Basem Nouh on 14 Nov 2020
ooops i haven't understood from 2 to 6
how can some one write those :?
it's not working :(
Walter Roberson
Walter Roberson on 14 Nov 2020
For (2), put all of these together in one file:
%some data, replace this with meaningful data
T1 = randn()*5;
T2 = randn()*5;
%the code
[zeta, z, teta]= newcomb(T1,T2);
R_praezession= praezession(zeta,z,teta);
function [zeta,z,teta]= newcomb(T1,T2)
zeta = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (0.30188 - 0.000344 * T1)...
*T2^2 + 0.017998 * T2^3) / 648000 * pi;
z = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (1.09468 + 0.000066 * T1) * T2^2 + 0.018203 * T2^3) / 648000 * pi;
teta = ((2004.3109 - 0.85330 * T1 - 0.000217 * T1^2) * T2 + (-0.42665 - 0.000217 * T1) * T2^2 - 0.041833 * T2^3) / 648000 * pi;
end
function R_praezession = praezession (zeta,z,teta)
Rz=[cos(-z) sin(-z) 0; -sin(-z) cos(-z) 0; 0 0 1];
Ry=[cos(teta) 0 -sin(teta); 0 1 0; sin(teta) 0 cos(teta)];
Rx_zeta=[cos(-zeta) sin(-zeta) 0; -sin(-zeta) cos(-zeta) 0; 0 0 1];
R_praezession = Rz*Ry*Rx_zeta;
end
For (4), put this in one file:
%some data, replace this with meaningful data
T1 = randn()*5;
T2 = randn()*5;
%the code
[zeta, z, teta] = astro('newcomb', T1, T2);
R_praezession = astro('praezession', zeta, z, teta);
and put these in astro.m:
function varargout = astro(choice, varargin)
switch choice
case 'newcomb':
[varargout{1:nargout}] = newcomb(varargin{:});
case 'praezession':
[varargout{1:nargout}] = prezession(varargin{:});
otherwise:
error('Unknown choice for astro()')
end
end
function [zeta,z,teta]= newcomb(T1,T2)
zeta = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (0.30188 - 0.000344 * T1)...
*T2^2 + 0.017998 * T2^3) / 648000 * pi;
z = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (1.09468 + 0.000066 * T1) * T2^2 + 0.018203 * T2^3) / 648000 * pi;
teta = ((2004.3109 - 0.85330 * T1 - 0.000217 * T1^2) * T2 + (-0.42665 - 0.000217 * T1) * T2^2 - 0.041833 * T2^3) / 648000 * pi;
end
function R_praezession = praezession (zeta,z,teta)
Rz=[cos(-z) sin(-z) 0; -sin(-z) cos(-z) 0; 0 0 1];
Ry=[cos(teta) 0 -sin(teta); 0 1 0; sin(teta) 0 cos(teta)];
Rx_zeta=[cos(-zeta) sin(-zeta) 0; -sin(-zeta) cos(-zeta) 0; 0 0 1];
R_praezession = Rz*Ry*Rx_zeta;
end
for (5), put this in one file:
%some data, replace this with meaningful data
T1 = randn()*5;
T2 = randn()*5;
%the code
afuns = astro();
[zeta, z, teta] = afuns.newcomb(T1, T2);
R_praezession = afuns.praezession(zeta, z, teta);
and put these in astro.m
function afuns = astro()
afuns = struct('newcomb', @newcomb; ...
'praezession', @praezession);
end
function [zeta,z,teta]= newcomb(T1,T2)
zeta = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (0.30188 - 0.000344 * T1)...
*T2^2 + 0.017998 * T2^3) / 648000 * pi;
z = ((2306.2181 + 1.39656 * T1 + 0.000139 * T1^2) * T2 + (1.09468 + 0.000066 * T1) * T2^2 + 0.018203 * T2^3) / 648000 * pi;
teta = ((2004.3109 - 0.85330 * T1 - 0.000217 * T1^2) * T2 + (-0.42665 - 0.000217 * T1) * T2^2 - 0.041833 * T2^3) / 648000 * pi;
end
function R_praezession = praezession (zeta,z,teta)
Rz=[cos(-z) sin(-z) 0; -sin(-z) cos(-z) 0; 0 0 1];
Ry=[cos(teta) 0 -sin(teta); 0 1 0; sin(teta) 0 cos(teta)];
Rx_zeta=[cos(-zeta) sin(-zeta) 0; -sin(-zeta) cos(-zeta) 0; 0 0 1];
R_praezession = Rz*Ry*Rx_zeta;
end
I will skip writing the two class-based approaches.

Sign in to comment.


Bruno Luong
Bruno Luong on 14 Nov 2020
Edited: Bruno Luong on 14 Nov 2020
Write them in 2 separate mfiles.
Only one function per file (the top one) is visible outside.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!