Matlab GUI wait until button is pressed

9 views (last 30 days)
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
  2 Comments
Berke Ogulcan Parlak
Berke Ogulcan Parlak on 1 May 2020
Sorry, the question is how can i provide a code like #wait here until startButton is pressed#

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 May 2020
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.

More Answers (2)

Adam Danz
Adam Danz on 1 May 2020
Edited: Adam Danz on 1 May 2020
Here's a conceptual plan you can implement to re-start the simulation at the point where it was stopped. If you are unsure how to interpret or implement one of the steps, leave a specific comment below and I can help you through it.
  1. Determine which variables should be saved after the simulation starts. In other words, after the simulation is paused, which variables contain data you'll need to start the simulated at the same position it was paused.
  2. Those variables should be listed at the top of your code and declared as persistent variables. Persistent variables store the values after the function is ended so next time the function is called, it retains their values.
  3. Toward the beginning of the function, determine whether the persistent variables are empty (see isempty). If the persistent variables are empty, the simulation should start from the beginning. If the persistent variables are not empty, the simulation should start where it left off. There are two ways to do that. First, you could redefine your for-loops based on the persistent variables. Second, you could use a condition at the top of the for-loops to skip to the next iteration until the loop-counter reaches whereever it left off. It would look something like this: if previousLoopCount <= theta2; continue; end.
  4. When the simulation ends or if you decide to restart the simulation, you must clear the persistent variables so that next time the function is called, it will start the simulation from the beginning.
  2 Comments
Berke Ogulcan Parlak
Berke Ogulcan Parlak on 2 May 2020
Everything is clear but you still haven't said how to stop the simulation when I press the "Stop" button?
Adam Danz
Adam Danz on 2 May 2020
Ah, right. The stop button should set a flag in the GUI from 1 to 0. The callback to the stop button will merely set this flag using guidata.
The loop that controls the simulation should check the value of this flag on each iteration. If the flag is 0, the simulation stops and then resets the flag to 1.

Sign in to comment.


Berke Ogulcan Parlak
Berke Ogulcan Parlak on 2 May 2020
Edited: Berke Ogulcan Parlak on 2 May 2020
I arrange the Demo of Image Analyst for my purpose. Thank you to everyone.
  1 Comment
Image Analyst
Image Analyst on 2 May 2020
If my answer solved your question, then could you please "Accept" my answer? Thanks in advance.

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!