Stuart’s MATLAB Videos

Watch and Learn

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 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


% calculating labda using the graph of vbar_i vs. vbar

if vbar(i)<=2
% plot(vbar,vbar_i)

% Matrices to solve theta0 and thetaC

A=[2/3+miu(i)^2 -miu(i);(8/3)*miu(i) -(1+((3/2)*(miu(i)^2)))];


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

hold on
dhull replied on : 6 of 8
@Ali, 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.