Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Capture break in mata


From   Phil Schumm <[email protected]>
To   <[email protected]>
Subject   Re: st: Capture break in mata
Date   Tue, 1 Oct 2013 15:14:28 -0500

On Oct 1, 2013, at 1:48 PM, George Vega Yon <[email protected]> wrote:
> I've tried coding somethign like this
> 
> mata:
> mata clear
> real scalar myfun() {
> 
>    /* Definitions */
>    real scalar i, j, bk;
>    real scalar x;
> 
>    /* Setting breakkey interrupt off */
>    bk=setbreakintr(0);
>    i=1;
>    while(!breakkey() & i   < 1e4)
>        for(j=1;j<=1000;j  ) x=i;
> 
>    /* Restoring breakkey and its mode */
>    breakkeyreset();
>    (void) setbreakintr(bk);
> 
>    return(x);
> }
> 
> myfun();
> 
> end
> 
> For which, once running, I press the key Break, hoping that the while loop stops, the x value stores the last -i- used and the breakkey behavior restores; and what I get this message: "Break disallowed at this time", and the program continues and returns 1e4 (what means that it stayed in the loop until i=1e4, i.e. the breakkey() value never changed from 0).


If you replace

    for(j=1;j<=1000;j  ) x=i;

with

    for(j=1;j<=1000;j++) x=i++;

you'll get the behavior you want.  Without incrementing j, you'll never leave the -for- loop, and without incrementing i, x will always equal 1 (instead of the number of times through the -while- loop).  Here is your code cleaned up a bit:

    real scalar myfunc() {
    
        real scalar bk, i, j
    	
        bk = setbreakintr(0)
        i = 1
        while (!breakkey() & i<1e4) {
            i++
            for (j=1;j<=1000;j++) {
                <stmts>
            }
        }
    	
        breakkeyreset()
        (void) setbreakintr(bk)
    	
        return(i)
    }


-- Phil


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index