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: Unexpected end of file in mata.
From
"William Gould, StataCorp LP" <[email protected]>
To
[email protected]
Subject
Re: st: Unexpected end of file in mata.
Date
Mon, 27 Jan 2014 10:18:24 -0600
Amadou Diallo <[email protected]> has yet more questions on Mata,
and good ones.
> I am a bit surprised to learn that mata/stata interprets "if (i=2) {"
> as "whether 2 is not equal to 0." (I'd have thought equal to 2!).
Think like this:
a = b = 2
assigns the value 2 to a and b. That happens because
a = b = 2 is equivalent to typing a = (b=2)
Thus, (b=2) returns 2, and that's how a is set equal to 2.
Okay, we've established that the expression (b=2) returns 2.
So now consider
if (b=2) {
...
}
The statement (b=2) is true because b=2 returns 2 and Mata interprets
0 to mean false and all other values to mean true.
> I couldn't find a way to ask mata to check whether x was indeed a
> colvector...
A colvector has one column. Ergo,
if (cols(a)==1) {
/* a is a column vector */
}
Other useful functions in checking what a variable contains
include rows(), length(), eltype(), isreal(), iscomplex(), isstring(),
and ispointer().
> More generally, when a mata program aborts (with a error code as it
> seems to me it is almost always the case), how to capture that error
> code, so that we use that _rc as in stata mode?
When somethiung goes wrong, Mata aborts the execution and presents
an ugly traceback log, a process we summarizes by saying that the
subroutine aborts with error. Abort with error is useful for
debugging, but not so useful for professional code where you want to
present an error message and show the return code.
The functions you want to learn about are errprintf(), error(), and
exit().
Let's consider the following code
... mysubroutine(...)
{
...
stata("summarize + " user_specified_varname)
...
}
In this program, we want to run Stata's -summarize- command. Let's
imagine what happens when the user_specified_varname is not found.
The way stata() works, if the commnd fails, it aborts with error.
Here's alternative code that exits gracefully in the error case:
... mysubroutine(...)
{
real scalar rc
...
if ((rc=_stata("summarize + " user_specified_varname)) {
exit(rc)
/*NOTRECHED*/
}
...
}
_stata() is the function that does what stata() does, but rather than
aborting with error if the Stata command fails, returns the return
code from the Stata command in all cases. _stata() returns 0 when
the command executed without error. It returns nonzero when there
was a problem.
exit() is the function that terminates execution (without traceback log)
and sets the return code to the specified argument.
Oh yes, and notice that I used expression (rc=...) with the single equal
sign. I could have coded the expression ((rc=...)!=0).
I didn't have to call Stata to get a return code. Here's another
example:
... myothersubroutine(...)
{
...
if (cols(x)!=1) {
exit(error(503))
}
...
}
error(503) presents the error message associated the return code 503,
and it returns 503. exit() exits cleanly -- stops execution -- with
the specified return code. Thus, the user will see
. whatever ...
conformability error
r(503);
Here's a version that's more customized:
... myothersubroutine(...)
{
...
if (cols(x)!=1) {
(void) error(503)
errprintf("{p 4 4 2}\n")
errprintf("%d x %d found where column vector expected.\n",
rows(x), cols(x))
errprintf("{p_end}\n")
exit(503)
}
...
}
The user will see
. whatever ...
conformability error
2 x 4 found where column vector expected.
r(503);
-- Bill
[email protected]
*
* 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/