MATLAB Answers

How to call a private function in a static function in App Designer?

조회 수: 13(최근 30일)
Laurian Hurduza
Laurian Hurduza 2021년 5월 4일
댓글: Steven Lord 2021년 5월 4일
Hello,
I want to make an app in Matlab App Designer to take some values and show some plots acordingly.
I made a simple interface with some plots, some edit fields and a plot button.
I want to take value from edit fields ( values that are typed manually by the user ) and after to plot. After I modify the values in edit fields and press plot button again, I want to see the changes that occur when I changed the variables, no matter how many times I edit values in edit fields and press the button.
The problem is that I can't have it to get working.
I created a function called func2(app) and I want to call it in my static function called anaerob(~,x).
In func2 I have this:
methods (Access = private)
function k1 = func2(app)
k1 = app.k1EditField.Value;
end
end
In this one I want to have the values that will be put later in edit fields.
And I want to declare it in my anaerob function:
methods ( Static )
function xd = anaerob(~,x)
xd=zeros(6,1);
func2(app)
k1=3.2;
k2=16.7; k3=1.035; k4=1.194; k5=1.5;
k6=3; k7=0.113; u10=0.2; u20=0.5; KM1=0.75; KM2=4; KI2=21;
c1=1; c2=1; Dd=0.05; S1in=10;
u1_S1=u10*(x(2)/(KM1+x(2)));
u2_S2=u20*(x(4)/(KM2+x(4)+x(4)^2/KI2));
fi1=u1_S1*x(1);
fi2=u2_S2*x(3);
cp=0.32;Qp=cp*x(6);
QCO2=1;
xd(1)=fi1-Dd*x(1); %X1
xd(2)=-k1*fi2-Dd*x(2)+ Dd*S1in; %S1
xd(3)=fi2-Dd*x(3); %X2
xd(4)=k3*fi1-k2*fi2-Dd*x(4); %S2
xd(5)=k4*fi1+k5*fi2-Dd*x(5)-QCO2; %S3
xd(6)=k7*fi1+k6*fi2-Dd*x(6)-cp*x(6); %P
end
end
This is the plot button callback:
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PLOTButton
function PLOTButtonPushed(app, event)
t0=0;tf=30;x0=[20 9 5 32 10 10];
[~,x]=ode45(@app.anaerob,[t0 tf],x0);
plot(app.UIAxes,x(:,1))
plot(app.UIAxes_2,x(:,2))
plot(app.UIAxes_3,x(:,3))
plot(app.UIAxes_4,x(:,4))
plot(app.UIAxes_5,x(:,5))
plot(app.UIAxes_6,x(:,6))
clear; close all;
end
end
Thank you in advance for help!

채택된 답변

Steven Lord
Steven Lord 2021년 5월 4일
Nowhere in your static method have you defined the variable app.
methods ( Static )
function xd = anaerob(~,x)
xd=zeros(6,1);
func2(app)
Generally static methods should not depend on the state of the app itself (or require an instance of the app as input.)
As an example, if I had a Human class the speak method would NOT be Static because different instances of Human speak in different ways (hello, hola, hi, how you doin?, etc.) The isInSolarSystem method could be Static at least for right now, since as far as we know every Human is in the Solar system right now.
  댓글 수: 2
Steven Lord
Steven Lord 2021년 5월 4일
Generally speaking you probably shouldn't try using a class method as the ODE function for a call to one of the ODE solvers like ode45. ode45 has a strict requirement for what inputs the ODE function accepts (a time value and a vector of state values) and your class method doesn't satisfy those requirements.
Instead, as long as you don't need your ODE function to be visible anywhere outside the class I would make it a class-related function after the end of the class definition. If that's not possible or if you need to make it visible I would make it a Static method (in which case you'd call it by <name of your app>.<name of static method> and write it so it accepts just time and state) or as a separate function in the directory containing your class definition.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by