Stuart’s MATLAB Videos

Watch and Learn

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the Original version of the page.

How to loop until a button is pushed in MATLAB 8

Posted by Doug Hull,

I had a question recently about having a process occur until a button is pressed in a GUI (or while a button is pressed). This video shows how a button can control the output of a stream of random numbers.

8 CommentsOldest to Newest

Trond Danielsen replied on : 1 of 8
This highlights one of the major limitations in Matlab. If you omit the pause in the callback you have a deadlock since the GUI would never refresh and you will not be able to stop the while-loop. If the callback, or should I call it the background task, is used to fetch data from an external source such as a web service or sensor then the GUI will remain unresponsive until the callback returns. I've used timer as a poor mans substitute for threads and then simply used the callback to start and stop the timer. Still this solution is limited by matlabs single thread implementation.
Paulo Silva replied on : 2 of 8
Your tutorial inspired me to do this https://www.mathworks.com/matlabcentral/fileexchange/29618-spspj the code isn't good but it works well and didn't take much time to do. Please keep posting great tutorials :)
dhull replied on : 3 of 8
@Trond I believe a drawnow would work in the situation you are describing also. Both have the effect of calling for a clearing of the graphics queue. @Paulo Thanks for adding to the File Exchange.
Ali replied on : 4 of 8
Hi, I have a problem with looping here is my program, there's no problem until defining A: <if vbar(i) all the steps give the expected answer which are 1*100 matrices. but suddenly at the matrix part (A&B) I just get a 2*2 for A and a 2*1 for B meaning the loop only evaluates the 100th number of miu and other variables, but I have different values of miu(i)[1*100 matrix] ?! what is the problem here? or is it a problem?! I dont know if the explanations are enough for you to understand the problem, if needed I'll describe more. regards,
Sorry it seems that the program wasn't sent here it is:
%Initial Data
rho=1.225;                    % Air density at 20 degrees Celcius(kg/m^3)
R=7.5;                        % Main blade radius(m)
N=4;                          % Number of blades
W=7400;                       % Gross weight(Kg)
omega=265*2*pi/60;            % main blade rotation(rad/sec)
Cl=5.73;                      % NACA 0012 lift-alpha coefficient(1/rad)
Aeq=16*(0.3048^2);            % Equivalent Area(m^2)
v=linspace(0,73,100);         % speed of Helicopter(m/sec)
sig=N/(omega*R);              % Solidity

for i=1:100

Dfus(i)=rho*(v(i)^2)*Aeq/2;
alphad(i)=Dfus(i)/W;
T(i)=sqrt(W^2+Dfus(i)^2);
CT(i)=T(i)/(rho*pi*(omega^2)*(R^4));
miu(i)=v(i)./(omega*R);

% calculating labda using the graph of vbar_i vs. vbar
vbar(i)=miu(i)./sqrt(CT(i)./2);

if vbar(i)<=2
    vbar_i(i)=sqrt(-(vbar(i)^2/2)+sqrt((vbar(i)^4/4)+1));
else
    vbar_i(i)=1/vbar(i);
end
% plot(vbar,vbar_i)

% Matrices to solve theta0 and thetaC

labda(i)=vbar_i(i)./(omega*R);
A=[2/3+miu(i)^2 -miu(i);(8/3)*miu(i) -(1+((3/2)*(miu(i)^2)))];
B=[(4*CT(i))./(Cl*sig)+labda(i)+(miu(i)*alphad(i));2*miu(i)*(labda(i)+miu(i)*alphad(i))];
theta(:,i)=A\B;

end

theta0=theta(1,i);    % Collective pitch
thetaC=theta(2,i);    % Cyclic pitch

plot(v,theta0,'g')
hold on
plot(v,thetaC,'r') 
dhull replied on : 6 of 8
@Ali, https://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/ I think this is what you are looking for.
Kent replied on : 7 of 8
Fantastic! Your comment at the end was exactly what I was looking for - to have separate start and cancel push buttons. Thank-you!
tarun siingh replied on : 8 of 8
I want to make a physical pushbutton on bread board by connecting with Arduino and interface this pushbutton to the Gui virtual pushbutton to call-back when I push the physical one. please, provide me coding help to do this task.