MATLAB: Matlab GUI wait until button is pressed

buttonguiMATLABpressedwaitwaitfor

Hello everyone, I am trying to simulate the four bar mechanism. There are two buttons: Start and Stop.The simulation will start when I press Start, and the simulation will stop until I press the Start button again.
Here is my GUI:
Here is my code:
% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
L1 = str2double(handles.L1.String);
L2 = str2double(handles.L2.String);
L3 = str2double(handles.L3.String);
L4 = str2double(handles.L4.String);
A=[0,0]; %A noktasının [x,y] konumları -fix olduğu için buraya yazıldı.
D=[L1,0]; %D noktasının [x,y] konumları -fix olduğu için buraya yazıldı.
%---------------------------------Auto Axis Scaler-------------------------------
k = 1;
for theta2 = 0:0.1:2*pi
BD=sqrt(L1.^2+L2*L2-2*L1*L2*cos(theta2)); %L1'in karesi=L1.^2


fi=acos((L3*L3+L4*L4-BD*BD)/(2*L3*L4));
theta3=2*atan((-L2*sin(theta2)+L4*sin(fi))/(L1+L3-L2*cos(theta2)-L4*cos(fi)));
theta4=2*atan((L2*sin(theta2)-L3*sin(fi))/(L4-L1+L2*cos(theta2)-L3*cos(fi)));
%2) Joint noktalarının konumları girilir.


B(k,:) = [(L2*cos(theta2)),(L2*sin(theta2))]; %B noktasının [x,y] konumları


C(k,:) = [(L2*cos(theta2)+L3*cos(theta3)),(L2*sin(theta2)+L3*sin(theta3))];
k = k+1;
end
Cx = C(:,1);
Cy = C(:,2);
Bx = B(:,1);
By = B(:,2);
Cx_min = min(Cx);
Cx_max = max(Cx);
Cy_min = min(Cy);
Cy_max = max(Cy);
Bx_min = min(Bx);
Bx_max = max(Bx);
By_min = min(By);
By_max = max(By);
F = [Bx_min,Cx_min];
G = [By_min,Cy_min];
upper_x = L1 + abs(Cx_max-L1) + (L1/10);
lower_x = min(F) - (L1/10);
upper_y = Cy_max + (L1/10);
lower_y = min(G) - (L1/10);
clear k Cx Cy Bx By Cx_min Cx_max Cy_min Cy_max
clear Bx_min Bx_max By_min By_max F G BD B C fi
clear theta3 theta4 theta2
%------------------------------------------------------------------------------------
axis(gca,'equal'); %Aspect ratio:Yani daha oranlı bir grafik görüntüsü sağlar.
axis([lower_x,upper_x,lower_y,upper_y]); %X ekseni -2.5'den 6'ye,Y ekseni ise -2.5'den 6'e kadar olsun.
for theta2 = 0:0.1:2*pi
%getappdata(handles.FuncName,'VarName');
J = getappdata(handles.stopButton, 'stop');
if J == 0
rmappdata(handles.stopButton, 'stop')
%1) Pozisyon analizi ile bulunan açı değerleri girilir.

BD=sqrt(L1.^2+L2*L2-2*L1*L2*cos(theta2)); %L1'in karesi=L1.^2
fi=acos((L3*L3+L4*L4-BD*BD)/(2*L3*L4));
theta3=2*atan((-L2*sin(theta2)+L4*sin(fi))/(L1+L3-L2*cos(theta2)-L4*cos(fi)));
theta4=2*atan((L2*sin(theta2)-L3*sin(fi))/(L4-L1+L2*cos(theta2)-L3*cos(fi)));
%2) Joint noktalarının konumları girilir.
B=[(L2*cos(theta2)),(L2*sin(theta2))]; %B noktasının [x,y] konumları
C=[(L2*cos(theta2)+L3*cos(theta3)),(L2*sin(theta2)+L3*sin(theta3))];
%3) Linkler oluşturulur.

l2_line=line([A(1),B(1)],[A(2),B(2)],'LineWidth',4,'Color','k');
l3_line=line([B(1),C(1)],[B(2),C(2)],'LineWidth',4,'Color','k');
l4_line=line([C(1),D(1)],[C(2),D(2)],'LineWidth',4,'Color','k');
%line([x1,x2],[y1,y2]) komutu x1,y1'den x2,y2 konumuna çizgi çeker.

%A(1) ifadesi,A vektörünün 1.elemanını ifade eder.

%LineWidth:Çizginin kalınlığı ayarlanabilir.

%Color:Çizginin rengi ayarlanabilir.

%4) Jointler oluşturulur.

a_joint=viscircles(A,(L2/20),'Color','r','LineWidth',6); %A joint temsili

b_joint=viscircles(B,(L2/20),'Color','r','LineWidth',6);
c_joint=viscircles(C,(L2/20),'Color','r','LineWidth',6);
d_joint=viscircles(D,(L2/20),'Color','r','LineWidth',6);
%viscircles(çemberin merkez konumu,yarıçapı);

%LineStyle= '-'Solid '--'Dashed ':'Dotted '-.'Dash-Dot

%5 Trajectory(varsa) oluşturulur.

C_traj=viscircles(C,(L2/2000),'Color','k'); %Trajectory of C

%-Wait here until startButton is pressed-
delete(a_joint);
delete(b_joint);
delete(c_joint);
delete(d_joint);
delete(l2_line);
delete(l3_line);
delete(l4_line);
end
%1) Pozisyon analizi ile bulunan açı değerleri girilir.
BD=sqrt(L1.^2+L2*L2-2*L1*L2*cos(theta2)); %L1'in karesi=L1.^2
fi=acos((L3*L3+L4*L4-BD*BD)/(2*L3*L4));
theta3=2*atan((-L2*sin(theta2)+L4*sin(fi))/(L1+L3-L2*cos(theta2)-L4*cos(fi)));
theta4=2*atan((L2*sin(theta2)-L3*sin(fi))/(L4-L1+L2*cos(theta2)-L3*cos(fi)));
%2) Joint noktalarının konumları girilir.
B=[(L2*cos(theta2)),(L2*sin(theta2))]; %B noktasının [x,y] konumları
C=[(L2*cos(theta2)+L3*cos(theta3)),(L2*sin(theta2)+L3*sin(theta3))];
%3) Linkler oluşturulur.
l2_line=line([A(1),B(1)],[A(2),B(2)],'LineWidth',4,'Color','k');
l3_line=line([B(1),C(1)],[B(2),C(2)],'LineWidth',4,'Color','k');
l4_line=line([C(1),D(1)],[C(2),D(2)],'LineWidth',4,'Color','k');
%line([x1,x2],[y1,y2]) komutu x1,y1'den x2,y2 konumuna çizgi çeker.
%A(1) ifadesi,A vektörünün 1.elemanını ifade eder.
%LineWidth:Çizginin kalınlığı ayarlanabilir.
%Color:Çizginin rengi ayarlanabilir.
%4) Jointler oluşturulur.
a_joint=viscircles(A,(L2/20),'Color','r','LineWidth',6); %A joint temsili
b_joint=viscircles(B,(L2/20),'Color','r','LineWidth',6);
c_joint=viscircles(C,(L2/20),'Color','r','LineWidth',6);
d_joint=viscircles(D,(L2/20),'Color','r','LineWidth',6);
%viscircles(çemberin merkez konumu,yarıçapı);
%LineStyle= '-'Solid '--'Dashed ':'Dotted '-.'Dash-Dot
%5 Trajectory(varsa) oluşturulur.
C_traj=viscircles(C,(L2/2000),'Color','k'); %Trajectory of C
pause(0.01); %Her 0.01 saniyede bir görüntü güncellemesi yapar
delete(a_joint);
delete(b_joint);
delete(c_joint);
delete(d_joint);
delete(l2_line);
delete(l3_line);
delete(l4_line);
%delete fonksiyonu sayesinde bi önceki adımda oluşan görüntü temizlenir
end
% cla : Eksenleri temizlemek için kullanılır!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
% --- Executes on button press in stopButton.
function stopButton_Callback(hObject, eventdata, handles)
%setappdata(hObject,VarName,Value)
setappdata( hObject, 'stop', 0 ) % stop = 0

Best Answer

I have this exact function in the attached very small demo. I've posted it many times before, and here it is again. Clicking Go and stop will start or pause the updating of a counter and its display on the GUI.