Is that the right way to do it? Very possibly there is no good way. If you are getting warning messages, that generally means your matrix is numerically singular. Finding the inverse of a numerically singular matrix will not be well posed, no matter what computation you use.
So the very first thing you need to do is test the condition number of the matrix. If it is truly very large and sparse, then condest may be the best tool, to give at least an estimate of the approximate condition number. Of course, a lot of people think their matrices are large and sparse, when they are neither truly large or truly sparse. So cond may suffice for you, to tell you if the matrix is singular. Again, if your matrix is singular, then you are wasting your time to compute the diagonal of the inverse, since the inverse matrix will be numerical garbage.
A better solution may depend on how the matrix was created, using a little mathematics. But that is something we are not able to know, since you have told us nothing of value.