NEED HELP! I'm trying to Create a plot in MATLAB App Designer that uses a variable (for the x-axis) that goes up in real time in seconds....
Afficher commentaires plus anciens
Hi everyone,
I am trying to create an App that plots a function/equation that uses a time variable. I wanted this time variable to go up in real time at the push of the start button within the GUI. The route that I took was to use a timer callback function for the start button so that when I press the start button the timer will go up with a period of 1 second (for the x-axis, going from 0 to 1 second..) and having that timer stop once you press the start button once again (this button will change from a start button to a stop button after clicking start...). However, when I start up the simulator and press start, I get no feedback from the graph and it produces 0 lines and 0 points (and the axes doesn't move..). It also doesn't give me any errors in my code view so that is why I am super confused and wondering if any of you coding pros can help me out. Also here is my code that I used for my function: (By the way, I tested the equation using an array of times already and the plot shows up just fine, just trying to figure out this real-time thing.)
properties (Access = private)
Timer % Description
end
methods (Access = private)
function TimerFcn(app,~,~)
%Constants and initial values:
L=23*(10^-6); %One group generation time [s]
beta=0.0075; %Delayed neutron fraction
lambda=0.077; %Decay constant
Cp=5*(10^5); %Reactor heat cap. [J/K]
alpha=-5*(10^-4); %Temp coefficient [drho/dK]
%
%Initial conditions:
%
T1 = 300; %Initial reactor temp. [K]
N1 = 100; %Initial reactor power [W]
C1 = ((beta*N1)/L)/lambda; %Initial precursor [W]
dollars = 2.00;
rho = dollars*beta; %Reactivity insertion
t= app.StartButtonPushed(); %Real Time Count
deltat = 0.001; %Time step [s]
i = 0; %Counting variable for watching iterations
k = 0;
for k = 1:length(t)
i = i+1; %Updating count
deltaN = funcdN(rho,beta,L,N1,lambda,C1); %Calculating change in power
N2 = N1 + (deltaN.*deltat); %Calculating the next projected power value
Navg=((N2+N1)./2); %Calculating the average power value
deltaE = Navg.*deltat; %Calculating energy released
deltaC = funcdC(beta,L,N1,lambda,C1); %Calculating change in concentration
C2 = C1+(deltaC.*deltat); %Calculating the next concentration value
Cavg = ((C2+C1)./2); %Calculating average concentration value
T2(k) = funcT(T1,deltaE,Cp); %Updating temp.
rho = rho+(alpha.*(T2-T1)); %Updating reactivity
N1 = Navg; %Storing power for next iteration
C1 = Cavg; %Storing concentration for next iteration
T1 = T2; %Storing temp. for next iteration
Power(k) = N1;
Power2(k) = log(N1);
Period(k) = deltat./(log(N2./N1));
period = deltat./(log(N2./N1));
end
k=1:length(t);
plot(app.UIAxes,Power(k));
grid on;
%User defined function
%Change in power
function dN = funcdN(rho,beta,L,N1,lambda,C1)
dN=((rho-beta)./L).*N1+(lambda.*C1);
end
%User defined function
%Change in precursor concentration
function dC = funcdC(beta,L,N1,lambda,C1)
dC = ((beta./L).*N1)-(lambda.*C1);
end
%User defined function
%Change in temperature
function Temp = funcT(T1,deltaE,Cp)
Temp = T1+(deltaE/Cp);
end
end
end
And here is my timer callback function for the start button:
function StartButtonPushed(app, event)
if app.StartButton.Text == "Start"
set(app.StartButton,"Enable","off")
pause(2)
set(app.StartButton,"Enable","on")
app.StartButton.Text = "Stop";
app.StartButton.BackgroundColor = [1,0,0];
app.Timer = timer('TimerFcn',@(x,y) app.TimerFcn(app),"Period",1,"ExecutionMode","fixedRate","StartDelay",2);
app.Timer.start()
app.StartButton.Text = "Stop";
else
set(app.StartButton,"Enable","off")
pause(2)
set(app.StartButton,"Enable","on")
app.Timer.stop()
app.StartButton.Text = "Start";
app.StartButton.BackgroundColor = [0,1,0];
delete(app.Timer);
end
1 commentaire
Elias Lynch
le 11 Avr 2022
Réponses (1)
Voss
le 11 Avr 2022
I'm not sure what the intent is with this line:
t= app.StartButtonPushed();
but I suspect that's where the error is happening, since StartButtonPushed doesn't have any output arguments.
You'll have to get value of the vector t in TimerFcn some other way.
3 commentaires
Walter Roberson
le 11 Avr 2022
I do not see that line in the posted code?
Elias Lynch
le 11 Avr 2022
Modifié(e) : Elias Lynch
le 11 Avr 2022
Walter Roberson
le 11 Avr 2022
When you start the timer, record datetime('now') in an app property. After that, at any point, the elapsed time of the timer is datetime('now') minus that stored value. You could have the startFcn do the storing.
timer objects do not inherently have an accessible start time.
t= app.StartButtonPushed(); %Real Time Count
even if you create a real-time count of some sort when you start the timer, then
for k = 1:length(t)
would not seem to make much sense ? Is the intent that t is the current time ? Is the intent that t would be a record of all the times that the timer has fired? You do not have an infinite loop that is going back and re-checking for new t so asking for length(t) would only deal with the values already stored in t .
... and that logic is inside your timer callback function.
I could imagine that there might be reason to have a timer function record [something or other], and that you also had a processing routine that checked for updates to that list and processed the updates -- but starting over from the original entries each time would not seem to make sense (the processing routine would get slower and slower as it had more to do each time and more and more of what it did would be re-calculating what had already been calculated.)
Catégories
En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!