Doug's MATLAB Video Tutorials

December 3rd, 2010

How to loop until a button is pushed in MATLAB

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.

6 Responses to “How to loop until a button is pushed in MATLAB”

  1. Trond Danielsen replied on :

    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.

  2. Paulo Silva replied on :

    Your tutorial inspired me to do this
    http://www.mathworks.com/matlabcentral/fileexchange/29618
    the code isn’t good but it works well and didn’t take much time to do.

    Please keep posting great tutorials :)

  3. dhull replied on :

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

  4. Ali replied on :

    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,

  5. Ali replied on :

    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')
    
  6. dhull replied on :

    @Ali,

    http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/

    I think this is what you are looking for.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.