Loren on the Art of MATLAB

Turn ideas into MATLAB


Loren on the Art of MATLAB has been archived and will not be updated.

Scalar Roulette

A while ago, Steve posted an article entitled Functional Design Clunkers on his blog. Well, I have a confession to make too. Have you ever been bitten by the ambiguity in the contour function?


Two Syntaxes That Can Clash

Here are the two syntaxes that can class for contour:

  • contour(Z,n)
  • contour(Z,v)

From the documentation:

contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. The number of contour levels is equal to length(v). To draw a single contour of level i, use contour(Z,[i i]).

Time for the confession. I thought at the time we were designing contour that asking for a single contour would be rare enough to warrant having the "convenient" syntaxes that could clash. And I even know the work-around for programmers. If they wanted to specify the contour levels, they simply needed some code like this:

         if length(mycontours) == 1
            mycontours(2) = mycontours;
         % now call the contour function

So, why don't I like this now? The code is harder to read, the intent is harder to discern, and it just feels clunky.

Possible Solutions

I can think of a bunch of possible solutions, were we to design the contour function today. These include (but certainly aren't limited to):

  • two functions, 1 for number of contours, 2 for values of contours
  • control number vs. level behavior with param/value pairs or some similar device
  • have one of the inputs (perhaps scalar level number) be in a cell array or some other class that differs from the class for the level values input

Have You Been Bitten?

Has the ambiguity in contour bitten you? Or one in some other MATLAB functionality? What solution do you prefer? Let me know here.

Published with MATLAB® 7.10

  • print


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.