GUI Plot using Timer Object and For Loops
Show older comments
I am trying to plot an equation in a GUI using timer object as as the variable for the x-axis. When I plot WITHOUT the for-loop, the GUI will plot a nice line while running continuously (to an infinite time or a large time 't') and you can see the red circle on the right edge of the plot. However I need to use the for-loop to correctly simulate the equation under given if/else conditions, otherwise it will just neglect the 'else' statement. The problem is that although the GUI will plot with the for-loop correctly, it seems to overwrite the existing data. Thus I won't see a curve profile of the equation but instead just a red circle on the right edge showing where the plot should be as time increases. How do I plot a GUI using timer objects with a for-loop and have it plot a nice curve without overwriting existing data to plot a nice single curve? (Perhaps using something similar to the "hold on" command?)
Below is the code for my timer callback functions that does all the calculations and plotting. I attached the GUI .fig and .m files for convenience
% code
function user_timercallback(obj,event,hObject)
handles=guidata(hObject);
handles.t=handles.t+0.01;
% Initial Conditions
Pin = 1; % Input Pressure (Pa) - Controlled by User
P1 = 0; % Initial Pressure 1 (Pa)
r0 = 0.0254; % Initial chamber radius (m)
dP1_shutoff = 1.2; % Shutoff Pressure (Pa)
Rst = 1; % Flow Resistance
V1 = (4/3)*pi*r0^3; % Initial Volume (m^3)
% Ideal Gas Law Variables
dn = 0.0000001; % moles/sec
R = 8.3145; % J/(mol K)
T = 293.15; % K
% Calculation of Pressure 1 with constant volume
for t=0:0.01:handles.t
if -dP1_shutoff < Pin - P1
P1 = (dn.*t.*R*T)./V1
else
Vdot = 0;
end
% % Calculation of Pressure 1 with varying volume
% for t=0:0.01:handles.t;
% if -dP1_shutoff < Pin - P1
% Vdot = (Pin - P1)./R;
% else
% Vdot = 0;
% end
%
% V1 = V1 + Vdot*t; % Updated volume of the chamber
% r = ((3./(4.*pi)).*V1).^(1./3); % Updated radius of the chamber
% P1 = 2*[1./(r0.^2.*r)].*[1-(r0./r).^6] % Updated Pressure 1
x = t;
y = P1;
set(handles.axes_myax,'xlim',[0 handles.t]);
set(handles.signal,'xdata',t,'ydata',y);
set(handles.dot,'xdata',t(end),'ydata',y(end));
guidata(hObject,handles);
end
Accepted Answer
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!