Coding challenge on input parsing
Jiro's pick this week is boundedline by Kelly Kearney. Today, there's also a fun challenge at the end!
Kelly is a veteran of the File Exchange, with a number of great entries, many of which have high ratings. I realized I had already picked one of her entries, legendflex. Going back and reading that post and looking through her code for legendflex, I realized why I like her entries. Kelly's programs are
- well-written, with great help.
- flexible, with many options.
- robust, with good error checking.
and you'll see what I mean. boundedline is a nice function that allows you to plot lines with shaded bounds, that can represent error or confidence bounds.
She acknowledges a few other File Exchange entries, a couple of which have been picked previously as a Pick of the Week. boundedline has additional capabilities that make it unique.
But today, I'd like to focus on one aspect of this entry, namely input parsing and error checking. As you can see from the help for the function, you can call boundedline in various ways:
[hl, hp] = boundedline(x, y, b) [hl, hp] = boundedline(x, y, b, linespec) [hl, hp] = boundedline(x1, y1, b1, linespec1, x2, y2, b2, linespec2) [hl, hp] = boundedline(. . ., 'alpha') [hl, hp] = boundedline(. . ., ax) [hl, hp] = boundedline(. . ., 'transparency', trans) [hl, hp] = boundedline(. . ., 'orientation', orient) [hl, hp] = boundedline(. . ., 'cmap', cmap)
There are some required inputs, some optional inputs, and some optional parameter-value inputs. In order to make this work, Kelly parses the inputs and makes sure that the inputs are all valid. But she doesn't use inputParser or validateattributes. I know that she knows about these functions, because she used them in her other entry, legendflex. Perhaps she didn't know about them when she wrote boundedline.
So my challenge to everyone is to come up with some code that would do the appropriate input parsing and error checking on the inputs using inputParser. Feel free to use various error-checking functions, such as validateattributes.
Now, I haven't done this myself, but I can envision for the most part how I would go about doing this. However, I'm struggling with the 3rd calling syntax above. So I'm going to make that an extra credit. Here are the rules/requirements:
- The function should be able to handle all, except the 3rd, calling syntax above. Any number of the optional arguments can be passed in.
- The input parameters should meet the respective criteria. (see help boundedline)
- All optional inputs should have default values. (see help boundedline)
- You can assume that the parameter-value pair inputs will be called after the other optional inputs.
- After parsing, the parameters should be put into a structure with fields "x", "y", "b", "linespec", "usealpha", "hax", "trans", "orient", "cmap".
- (Extra credit) Allow for the 3rd calling syntax.
That's it! Put your thinking hat on, and post your solutions here to win some MATLAB swag! I will write another post highlighting some of the solutions I get.
Please post the code answer as a function that takes in varargin as an input and returns strct (with appropriate fields) as an output. Be sure to use the "code" markup so that your code will be easier to read:
<pre class="code"> funtionc strct = validateIntputs(varargin) ... end </pre>
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.