pass data from app designer to .m file

I have a code was written in a .m file (the code below), i need to get the value of some parameters from another app that is created in app designer
i called the .m file in push button callback
the parameters i want to get are:
L
LOD
N
T_GMT
Step
so i write these valuse in edit field and return them to .m file
L=33; %Latitude
LOD=44; %Longitude
N=90; %day number
T_GMT=3; %Time difference with reference to GMT
Step=60; %step each hour
Ds=23.45*sin((360*(N-81)/365)*(pi/180)); % angle of
B=(360*(N-81))/364; %Equation of time
EoT=(9.87*sin(2*B*pi/180))- (7.53*cos(B*pi/180))- (1.5*sin(B*pi/180)); %Equation of time
Lzt= 15* T_GMT; %LMST
if LOD>=0
Ts_correction= (-4*(Lzt-LOD))+EoT; %solar time correction
else
Ts_correction= (4*(Lzt-LOD))+EoT; %solar time correction
end
Wsr_ssi=- tan(Ds*pi/180)*tan(L*pi/180); %Sunrise/Sunset
Wsrsr_ss=acosd(Wsr_ssi); %Sunrise/Sunset hour angle
ASTsr=abs((((Wsrsr_ss/15)-12)*60)); %Sunrise solar time
ASTss=(((Wsrsr_ss/15)+12)*60); %Sunset solar time
Tsr=ASTsr+abs(Ts_correction); %Actual Sunrise time
Tss=ASTss+abs(Ts_correction); %Actual Sunset time
sin_Alpha=[];
for LMT=Tsr:Step:Tss %for loop for the day time
Ts= LMT + Ts_correction; %solar time at each step
Hs=(15 *(Ts - (12*60)))/60; %hour angle at each step
sin_Alpha_i=(sin(L*pi/180)*sin(Ds*pi/180))+(cos(L*pi/180)*cos(Ds*pi/180)* cos(Hs*pi/180)); %altitudeangle
sin_Alpha=[sin_Alpha;sin_Alpha_i]; % store altitude
end
LMT=Tsr:Step:Tss;
sin_Alpha;
Go=1367; %solar constant
Gext=Go*(1+(0.0333*cos(360*N/365))); %available Gext
GextH=Gext*sin_Alpha; %Gex on horizontal surface
plot(LMT,GextH) %plot results

답변 (1개)

Rik
Rik 2020년 12월 27일

0 개 추천

The easy solution is to never use scripts for importants things. Put the code in a function so you can call it from anywhere, including an AppDesigner callback.
Don't forget to write comments that describe what the code is doing. You will not understand this in a year.
function [LMT,GextH]=your_function_name(L,LOD,N,T_GMT,Step)
%One line description goes here
%
%full explanation of what this function does goes here
%set up default values
if nargin<1
L=33; %Latitude
end
if nargin<2
LOD=44; %Longitude
end
if nargin<3
N=90; %day number
end
if nargin<4
T_GMT=3; %Time difference with reference to GMT
end
if nargin<5
Step=60; %step each hour
end
Ds=23.45*sin((360*(N-81)/365)*(pi/180)); % angle of
B=(360*(N-81))/364; %Equation of time
EoT=(9.87*sin(2*B*pi/180))- (7.53*cos(B*pi/180))- (1.5*sin(B*pi/180)); %Equation of time
Lzt= 15* T_GMT; %LMST
if LOD>=0
Ts_correction= (-4*(Lzt-LOD))+EoT; %solar time correction
else
Ts_correction= (4*(Lzt-LOD))+EoT; %solar time correction
end
Wsr_ssi=- tan(Ds*pi/180)*tan(L*pi/180); %Sunrise/Sunset
Wsrsr_ss=acosd(Wsr_ssi); %Sunrise/Sunset hour angle
ASTsr=abs((((Wsrsr_ss/15)-12)*60)); %Sunrise solar time
ASTss=(((Wsrsr_ss/15)+12)*60); %Sunset solar time
Tsr=ASTsr+abs(Ts_correction); %Actual Sunrise time
Tss=ASTss+abs(Ts_correction); %Actual Sunset time
sin_Alpha=[];
for LMT=Tsr:Step:Tss %for loop for the day time
Ts= LMT + Ts_correction; %solar time at each step
Hs=(15 *(Ts - (12*60)))/60; %hour angle at each step
sin_Alpha_i=(sin(L*pi/180)*sin(Ds*pi/180))+(cos(L*pi/180)*cos(Ds*pi/180)* cos(Hs*pi/180)); %altitudeangle
sin_Alpha=[sin_Alpha;sin_Alpha_i]; % store altitude
end
LMT=Tsr:Step:Tss;
sin_Alpha;
%^^^^^^^^
% what is this doing? sin_Alpha is a variable, so this will not do anything (because the ; will supress the printing)
Go=1367; %solar constant
Gext=Go*(1+(0.0333*cos(360*N/365))); %available Gext
GextH=Gext*sin_Alpha; %Gex on horizontal surface
end

댓글 수: 5

hello Rik thanks for your answer
I wrote this simple code just to get the idea of how to do it.
I wrote my main code in a script and now i want to use the app designer so i thought instead of rewriting the code in app desginer, i just call the .m file and pass the parameters
the parameters are: (they are all in edit fields):
t1
t2
vol
cp
Tac
mac
Amass
Tamb
cpwall
cpmass
mwall
mmass
emiss
abswall
absmass
trans
Aw
Ag
lat
long
gmt
dn
mint
tiltang_w
zs_w
tiltang_g
zs_g
this is my code:
t1=8; % start time
t2=15; % end time
h=((t2-t1)*3600)/60;
tspan=linspace(t1*3600,t2*3600,h);
IC=[32,33,35]; %intitial temperatures
[time,temp]=ode45(@sol,tspan,IC);
function tdot =sol(t,T)
t1=8;
t2=15;
h=((t2-t1)*3600)/60;
n=((t2-t1)*3600)/h;
vol=4;
mair=vol*1.225;
cp=1005;
Qh=0;%220;
Tac=15;
mac=0.09;
Uinwall=4;
Uoutwall=4.5;
Uinmass=12;
Amass=3;
Tamb=46;
cpwall=600;
cpmass=1450;
mwall=900;
mmass=50;
emiss=0.9;
Tsky=22.6;
abswall=0.19;
absmass=1;
trans=0.8;
Aw=[2.479 %roof
0.864 %windsheild
0.787 %rear window
1.533+0.224+0.266 %right side
1.533+0.224+0.266]; %left side
Ag=[0.864 %windsheild
0.787 %rear window
0.224+0.266 %right windows
0.224+0.266];%left windows
Awall=sum(Aw,'all');
AHwall=Aw(1,:);
lat=33.3152;
long=44.3661;
gmt=3;
dn=212; % day of the year
a=1160+75*sind((dn-275)*360/365); %constants of the equation
b=0.174+0.035*sind((dn-100)*360/365); %constants of the equation
c=0.095+0.04*sind((dn-100*360/365)); %constants of the equation
delta=23.45*sind((dn+284)*360/365); % Declination angle
bn=(dn-1)*360/365;
mint=0;
tiltang_w=[0
60
45
90
90];
zs_w=[0
0
180
90
270];
tiltang_g=[60
45
90
90];
zs_g=[0
180
90
270];
costhetaz=[];
costheta_w=[];
costheta_g=[];
for hour=t1:1/n:t2-1/60
lot=hour+mint/60;%local time of the city
eot=2.2918*(0.0075+0.1686*cosd(bn)-...
3.2077*sind(bn)-1.4615*cosd(2*bn)-...
4.089*sind(2*bn)); % equation of time
lst=15*gmt; % local standard time
st=(lot+(eot/60)+(long-lst)/15); % solar time
omega=15*(12-st); % hour angle
costhetaz_i=cosd(delta).*cosd(lat).*cosd(omega)+...
sind(delta).*sind(lat); % cosine of the zenith angle
costheta_wi=sind(delta).*sind(lat).*cosd(tiltang_w)-...
sind(delta).*cosd(lat).*sind(tiltang_w).*cosd(zs_w)+...
cosd(delta).*cosd(lat).*cosd(tiltang_w).*cosd(omega)+...
cosd(delta).*sind(lat).*sind(tiltang_w).*cosd(zs_w).*cosd(omega)+...
cosd(delta).*sind(tiltang_w).*sind(zs_w).*sind(omega);
costheta_gi=sind(delta).*sind(lat).*cosd(tiltang_g)-...
sind(delta).*cosd(lat).*sind(tiltang_g).*cosd(zs_g)+...
cosd(delta).*cosd(lat).*cosd(tiltang_g).*cosd(omega)+...
cosd(delta).*sind(lat).*sind(tiltang_g).*cosd(zs_g).*cosd(omega)+...
cosd(delta).*sind(tiltang_g).*sind(zs_g).*sind(omega);
costhetaz=[costhetaz;costhetaz_i];
costheta_w=[costheta_w;costheta_wi];
costheta_g=[costheta_g;costheta_gi];
end
idir=a.*exp(-b./costhetaz); % direct solar radiation
idif=c.*idir;
i_tot=idir+idif;
costheta_w(costheta_w<0)=0;
costheta_g(costheta_g<0)=0;
rad_w=zeros(5,h);
for i=1:h
for j=1:5
rad_w(j,i)=rad_w(j,i)+ i_tot(i,1).*costheta_w(j,1);
end
end
rad_g=zeros(4,h);
for i=1:h
for j=1:4
rad_g(j,i)=rad_g(j,i)+ i_tot(i,1).*costheta_g(j,1);
end
end
totalrad_w=zeros(5,h);
totalrad_g=zeros(5,h);
for i=1:h
for j=1:1:5
totalrad_w(j,i)=totalrad_w(j,i)+rad_w(j,i).*Aw(j,1);
end
end
totalrad_w=sum(totalrad_w,1);
for i=1:h
for j=1:1:4
totalrad_g(j,i)=totalrad_g(j,i)+rad_g(j,i).*Ag(j,1);
end
end
totalrad_g=sum(totalrad_g,1);
Qabs_t = linspace(t1*3600,t2*3600,h);
Qtrans_t = linspace(t1*3600,t2*3600,h);
Qabssolar=abswall*totalrad_w;
Qtranssolar=trans*absmass*totalrad_g;
Qabssolar = interp1(Qabs_t, Qabssolar, t);
Qtranssolar = interp1(Qtrans_t, Qtranssolar, t);
Q_ac=0;%(mac*cp*(Tac-T(1)));
Qin_w=(Uinwall*Awall*(T(1)-T(2)));
Qin_m=(Uinmass*Amass*(T(1)-T(3)));
Qout_w=(Uoutwall*Awall*(Tamb-T(2)));
Qin_rad=(5.67e-8*Amass*((T(3)+273)^4-(T(2)+273)^4));
Qout_rad=(5.67e-8*emiss*AHwall*((T(2)+273)^4-(Tsky+273)^4));
tdot=[(Qh+Q_ac-Qin_w-Qin_m)/(mair*cp)
(Qin_w+Qout_w+Qin_rad+Qabssolar-Qout_rad)/(mwall*cpwall)
(Qin_m+Qtranssolar-Qin_rad)/(mmass*cpmass)];
end
My answer is still the same: don't write code in a script, put it in a function.
For this large number of parameters I would suggest a struct. That way you can easily use a struct to contain all default values and loop over the fields in the input to replace default values. I put a short example below.
function out=functionWithComplexInput(struct_opts)
persistent default
if isempty(default)
default=struct;
default.time=0:100;
default.space=0:10;
end
opts=default;
if nargin>0
fn=fieldnames(struct_opts);
for n=1:numel(fn)
opts.(fn{n})=struct_opts.(fn{n})
end
end
%actual code here
end
Hasan Abbas
Hasan Abbas 2020년 12월 27일
thank you Rik! I will search for how to do that
Rik
Rik 2020년 12월 27일
You shouldn't need to search: you can use the examples I posted here. Let me know if you need help anyway.
Hasan Abbas
Hasan Abbas 2020년 12월 27일
ok, i will try it and tell you if i have any problem with it
thanks again

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 12월 27일

댓글:

2020년 12월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by