I came across very inconsistent error messages while writing a program which picks a maximum given a set of conditions. The bug (?) appeared when a subscript was invalid, mata returned the correct error code but pointed to a _editvalue() several lines above. Figuring the error out took me some time because of the inconsistent messages. When I write down the function in a sandbox, mata also returns inconsistent error messages.
A working example (just an example!) of the code is:
Clearly, the code should fail if the maximum is the last element in mat. This is a coding error. I would expect the error message that a subscript in invalid.
If I run
The message is confusing in several ways. First of all the function which reports the error is_editvalue(), but mata tells me the right number of lines skipped. Secondly the code after (!) _editvalue runs. Obviously, the error is that the subscript in if (mat[max_indic+1,1] == . ) check1 = 0 is invalid. So part of the error message is correct, it just points to the wrong function.
If I replace _editvalue(mat,0,.) with rnormal(max_indic,1,0,1) then mata returns
So pointing again to the wrong function.
I then checked the code outside of a function, so I coded:
Here the error message became:
I somewhat understand that mata has a problem because mat[max_indic+1,1], but why does it report unexpected end of line? Should it not realise that it is a invalid subscript?
If the code is instead:
or
Then the error message is "correct":
My question is, what is the reason for the difference if the code is within a function or not? I do know that mata compiles functions first, but why does the error point to the wrong command? In the sandbox mode out of a function, the error leads to different messages as well.
Any help would be highly appreciated.
A working example (just an example!) of the code is:
Code:
mata:
function checkBug(real matrix mat)
{
N = rows(mat)
maxindex(mat,1,max_indic=.,tmp=.)
_editvalue(mat,0,.)
"msg after _editvalue()"
if (mat[max_indic+1,1] == . ) check1 = 0
return(max_indic)
}
If I run
Code:
: checkBug((1,2,3,4,5,6)')
msg after _editvalue()
_editvalue(): 3301 subscript invalid
checkBug(): - function returned error
<istmt>: - function returned error
(1 line skipped)
If I replace _editvalue(mat,0,.) with rnormal(max_indic,1,0,1) then mata returns
Code:
rnormal(): 3301 subscript invalid
I then checked the code outside of a function, so I coded:
Code:
mata:
mat = (1,2,3,4,5,6)'
N = rows(mat)
maxindex(mat,1,max_indic=.,tmp=.)
_editvalue(mat,0,.)
if (mat[max_indic+1,1] == . ) check1 = 0
end
Code:
unexpected end of line (0 lines skipped)
If the code is instead:
Code:
mata:
mat = (1,2,3,4,5,6)'
N = rows(mat)
maxindex(mat,1,max_indic=.,tmp=.)
_editvalue(mat,0,.)
(mat[max_indic+1,1] == . )
end
Code:
mata:
mat = (1,2,3,4,5,6)'
N = rows(mat)
maxindex(mat,1,maxi=.,tmp=.)
_editvalue(mat,0,.)
i = 0
check = 0
while (check==0) {
i++
max_indic = maxi[i,1]
if (mat[max_indic+1,1] == . ) check1 = 0
}
end
Code:
<istmt>: 3301 subscript invalid (0 lines skipped)
Any help would be highly appreciated.

Comment