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   James Fiedler <jrfiedler@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Capture break in mata
Date   Tue, 1 Oct 2013 18:25:12 -0500

I also tried

real scalar myfunc() {
    real scalar bk, i, j

    bk = setbreakintr(0)
    i = 0
    while (!breakkey() & i++ < 1e8) {
        for (j = 1; j <= 1e4; j++) {
        }
    }

    breakkeyreset()
    (void) setbreakintr(bk)

    return(i)
}

and couldn't get it to work. When I hit the break button, I get a
popup saying "Break disallowed at this time". Maybe this depends on
some setting in Stata?

I did get something similar to work using my capture function. I
pasted the following into Stata. The code hangs on the loop inside of
capture() (as intended), and pressing the break button gives a return
value of 1. The i on the last line evaluates to a non-zero value.

i = 0

void while_func()
{
    external real scalar i

    i = 0
    while (!breakkey() & i++ < 1e8) {
        for (j = 1; j <= 1e4; j++) {

        }
    }
}

capture(&while_func(), J(0,0,.))
i



James

ps. If anyone is going to use the capture function, they should add the line

capture.func_ptr = NULL  // release func_ptr

right before "return(rc)".

On Tue, Oct 1, 2013 at 4:49 PM, Phil Schumm <pschumm@uchicago.edu> wrote:
> On Oct 1, 2013, at 4:28 PM, George Vega Yon <g.vegayon@gmail.com> wrote:
>> Phill, thanks for the correction. I'm not sure why but my text editor erased the "++" symbols both from the -while- and the -for- (that's why you can see two blank spaces right after -i- and -j-) Still, I don't get what I want.
>
>
> I'm fairly certain that the code I gave will do what you want.  Looking back at your original code, and plugging in the missing incrementers, I believe the problem is simply that the loop executes to completion so quickly that your break is not hitting inside the loop.  Try bumping up the max iterations of the -while- loop to 1e5 or the limit of the inner loop to 1e5, and you should get the behavior you're expecting.
>
>
> -- 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