MATLAB: Is a flag set on the possibility of a singular matrix

MATLAB

I'm using the equation solver A=B\C . The matrix can become singular and MATLAB sends out a 'warning' to this effect. 
Is there an associated flag that is set also in this case so I can make some decisions based on the possibility of a singular matrix?

Best Answer

To obtain the last warning issued by MATLAB, you can use the "lastwarn" function. A possible method to use the function is as follows:
1. Invert a matrix which becomes singular: 
>>A = inv([1 1; 1 1]);
2. Use the "lastwarn" function to obtain the warning message and ID:
>> [m,id]=lastwarn()
3. Check if "id" is equal to "MATLAB:singularMatrix":
>> strcmp(id,'MATLAB:singularMatrix')
The documentation for the "lastwarn" function can be found here:
 
Depending on the application, a good practice before operating on a matrix is to check its condition number. A very large value of condition number indicates a poorly conditioned matrix. You can find the condition number of a matrix by using the “cond” function.
The documentation for the “cond” function can be found here:
Related Question