Tuesday, January 14, 2014

On the practice of turning off all Matlab warnings, and how to avoid our most common one.

- Gautham

warnings off

It is common practice to turn off all matlab warnings, because often programs are written that produce them even under normal usage.

Warnings are there to alert the user that the program is doing something that the user may not have intended, or that this operation relies on some Matlab feature that is scheduled to change or disappear in a future version.

If you turn off warnings it means you don't want any help from any Matlab program. 

You don't want it to tell you that perhaps you may not have wanted the result it just gave you, or that the operation you just requested is unusual and that you may have made a mistake.

Think twice! 

Would you imagine turning off all warnings in your car because you are annoyed by one of them? Suppose you're one of the daring few who likes to drive without a seat belt. Are you equally sure you'll never leave the keys in the ignition?

There is almost always a way to tell the program, if the warning is not of concern, that yes, you do intend for that result, or that, yes, you are prepared for that eventuality.

As an example, the most common reason in our lab for the billowing forth of warnings is when you run imshow:

>> figure
>> imshow(rand(1024,1024))
Warning: Image is too big to fit on screen; displaying at 50%
> In imuitools/private/initSize at 72
  In imshow at 283 

Why is it doing this? Matlab thinks you may assume that the image is being displayed at 100% magnification (the default), which it will if the screen is big enough, and it wants you to know that you are looking at a scaled version. That is very nice of Matlab.

Here is the simple fix for imshow, quickly obtained by searching the web, and clearly stated in the documentation for the function:

>> figure
>> imshow(rand(1024,1024), 'InitialMagnification', 'fit')

That's it! Now it knows that you'll be happy with the scaled result because you told it specifically that you'll be ok with it.

If you cannot find a way to write your program so that Matlab is clear that it is giving you what you intended, you can turn off the specific warning you want to ignore. In the rare case that even that is not possible, you can turn off all warnings temporarily and then return warning reporting behavior back to what it was before you turned them off, which is a trickier task than one might think. (Scenario: you turn warnings off inside of a function and before your function had a chance to turn them back on some statement in between returns an error. What happens?)

See this reference on how to temporarily disable some or all warnings:


No comments:

Post a Comment