We're writing some data visualization code to compare different experiments. Each experiment is stored in a structure — let's say they're called E1, E2, E3… This let's us get at all the metadata for each structure, keep track of power, frequency, all that jazz. This has been working really really well for us and we have a lot of data stored this way.
I've been using varargin to load any number of these structures into our custom plotting functions. However, as we go forward, we need to have a few more optional parameters for some datasets. An example: experiments E1, E2, and E3 were measured at temperatures between [0 300] but experiments E4, E5, E6 were measured between [100 200]. I only want to plot the data from all experiments in the range [100 200] and I want to pass this as an optional parameter. This really seems to call for use of the inputParser class to parse the inputs and define defaults. I'll define a parameter 'TempRange',[## ##] and then there'll be a default if the user doesn't specify it. There are a couple of other options that we want to add this way too.
However, I do not know how many structures the end user will specify. In the above example, there were six. There could be 2 or 20. Using the addOptional method requires that I name the classes, but I don't know how many there will be. I could try to use KeepUnmatched to catch the extra parameters, but this is acting weirdly because I am trying to pass structures. If StructExpand is true, I lose my "E2" top-level structure and my fields get smushed together. If E2 and E3 have the same fieldnames (and this could sometimes happen) then the parsing function only keeps the last fields loaded. If StructExpand is false, I get the error "Expected a string for the parameter name, instead the input type was 'struct'."
Not sure why this is happening or the best way to try to load my variables. Any help out there? Short version of my code is below:
function myInputTest(input1,varargin)p = inputParser;p.KeepUnmatched = true;p.StructExpand = false;addRequired(p,input1,@(x) isstruct(x));parse(p,input1,varargin{:})
call
myinputTest(struct(),struct())
Although in this example I'm calling with empty structures, I get the same error if there are dummy fields in the structs.
Best Answer