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]

st: Capture break in mata


From   George Vega Yon <[email protected]>
To   [email protected]
Subject   st: Capture break in mata
Date   Tue, 1 Oct 2013 15:48:48 -0300

Dear Stata listers,

I've been reading Mata's documentation and I cannot find a way to
simply of working with -capture- block of code. I know that in Stata
one can create -capture- blocks such as

capture {
    ...code...
}

if (_rc) {
    ...code....
}

But I haven't seen something like that in Mata. What I'm actually
looking for is to, from within Mata, run a -while- loop and, if the
break key is pressed, do something.

Checking functions -setbreakintr()-, -resetbreakkey()- and
-breakkey()- I know that I can suppress the break key from
interrupting the program, but as I change its behavior the
-breakkey()- function doesn't captures if the break key has been
pressed (which is what I understand from the Diagnostics section in
the help file).

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

I know that -setbreakintr(0)- is intended to be used in the same way
that Stata's -nobreak- blocks, but is there any way that I can work
this out using solely Mata?

Thank you and excuse for any nonsense that I'm probably saying.

Best regards,

George Vega Yon
7 647 2552
http://cl.linkedin.com/in/georgevegayon
*
*   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