Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Using mata in semi-do-file / semi-interactive mode


From   Bert Lloyd <bert.lloyd.89@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Using mata in semi-do-file / semi-interactive mode
Date   Mon, 19 Mar 2012 14:19:56 -0400

Dear Nick,

Thanks for your helpful reply.

I did some investigating and it seems that my mistake was working
mostly with void functions. Apparently objects created within these
functions are not retained after a do-file ends or an error occurs.

Two contrasting MWEs are appended below for future readers.


**** MWE1.do: working within void function -- objects not retained
after error ***

--- begin do-file MWE1.do ---
clear all
mata:
	void ShowNonconformity()
	{
	my_colvec = J(2,1,1)
	my_colvec
	my_rowvec = J(1,2,1)
	my_rowvec
	non_conformable = my_colvec + my_rowvec
	}
end

mata: ShowNonconformity()
--- end do-file MWE1.do

--- screen output ---
[ run MWE1.do ]
. do MWE1.do

. clear all

. mata:
------------------------------------------------- mata (type end to exit)
>  ----------------------------------------------------------------------
:         void ShowNonconformity()
>         {
>         my_colvec = J(2,1,1)
>         my_colvec
>         my_rowvec = J(1,2,1)
>         my_rowvec
>         non_conformable = my_colvec + my_rowvec
>         }
note: variable non_conformable set but not used

: end
-------------------------------------------------------------------------

.
. mata: ShowNonconformity()
       1
    +-----+
  1 |  1  |
  2 |  1  |
    +-----+
       1   2
    +---------+
  1 |  1   1  |
    +---------+
     ShowNonconformity():  3200  conformability error
                 <istmt>:     -  function returned error
r(3200);

end of do-file

r(3200);

[go into mata to see what went wrong]

. mata
------------ mata (type end to exit) ------------------
: my_colvec
                 <istmt>:  3499  my_colvec not found
r(3499);

: my_rowvec
                 <istmt>:  3499  my_rowvec not found
r(3499);

: end
.
--- end screen output ---

**** MWE2.do: same mata code but not within a function -- objects are
retained after error ***

--- begin do-file MWE2.do ---
clear all
mata:
	
	my_colvec = J(2,1,1)
	my_colvec
	my_rowvec = J(1,2,1)
	my_rowvec
	non_conformable = my_colvec + my_rowvec
	
end
--- end do-file MWE2.do ---

--- screen output ---
. do MWE2.do

. clear all

. mata:
------------------------------------------------- mata (type end to exit)
>  ----------------------------------------------------------------------
:
:         my_colvec = J(2,1,1)

:         my_colvec
       1
    +-----+
  1 |  1  |
  2 |  1  |
    +-----+

:         my_rowvec = J(1,2,1)

:         my_rowvec
       1   2
    +---------+
  1 |  1   1  |
    +---------+

:         non_conformable = my_colvec + my_rowvec
                 <istmt>:  3200  conformability error
(1 line skipped)
-------------------------------------------------------------------------
r(3200);

end of do-file

r(3200);

[go into mata to see what went wrong]

. mata
------------------------------------------------- mata (type end to exit)
>  ----------------------------------------------------------------------
: my_colvec
       1
    +-----+
  1 |  1  |
  2 |  1  |
    +-----+

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index