GUI Plot using Timer Object and For Loops
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Geoff Hayes
2016년 4월 10일
Gary - note how you update the line object for the signal as
set(handles.signal,'xdata',t,'ydata',y);
where t and y are initialized as scalars. So the existing XData and YData is replaced with the new value and all previous history is lost. You would need to do something like
xData = [get(handles.signal,'XData') x];
yData = [get(handles.signal,'YData') y];
set(handles.axes_myax,'xlim',[0 handles.t]);
set(handles.signal,'xdata',xData,'ydata',yData);
to ensure that the history is preserved.
I did notice a potential problem. There is a for loop in the timer callback. Is this intentional because it seems to pollute the x data as it is constantly reset to zero (due to the loop) and so we have "waves" in the data.
댓글 수: 6
Geoff Hayes
2016년 4월 11일
Then you don't need the for loop. Since you last ended at handles.t, then use handles.t + 0.01 for the current timer callback.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!