Problem with passing Optional Arguements as a structure.
Show older comments
I'm having problems with using optional arguments as a structure. At the bottom, I've included the relevant portions of a function. I use this function as a subfunction from two different master functions. In one case it works fine, in the other I get errors that look like
Error using plot_edot (line 35) Argument 'LineWidth' failed validation with error: Expected input to be one of these types:
_ double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64_
Instead its type was struct.
The problem is not actually with the Linewidth arguement, as the Linewidth value is assigned with identical code in the two master functions and I can change which argument is reported as having a problem by changing the order of the p.addOptional commands.
Clearly, there is something wrong in how I'm handling the structure in this routine, but I can't see it. Hopefully someone more experienced can tell me what I'm missing.
Raph
function [axis_id] = plot_edot (edot, time, varargin)
%--------------------------------------------------------------------------
%[] = plot_mass_fraction (nuc_name, edot, time, ...)
% Plots mass fractions vs. time for provided data.
% Inputs: nuc_name: array of species names
% xmf: time dependent array of mass fractions
% time: array of times
% SeparateLegend: draw separate legends for each lineset
% LineStyle: line style of mass fraction lines
% LineWidth: width of mass fraction lines
% Outputs: axis_id: handle of current axis
%--------------------------------------------------------------------------
% Create an instance of the inputParser class.
p = inputParser;
% Define required inputs
p.addRequired('edot',@(x)validateattributes(x, {'numeric'}, {'vector', ...
'real'}));
p.addRequired('time',@(x)validateattributes(x, {'numeric'}, {'vector', ...
'real', '>=', 0}));
% Define optional inputs
p.addOptional('LineWidth', 2, @(x)validateattributes(x, {'numeric'}, ...
{'scalar', 'real', 'positive', '>=', 0.1, '<=', 10}))
p.addOptional('LineStyle', '', @ischar)
p.addOptional('LineColor', 'k',@ischar)
p.addOptional('TimeFromEnd',false,@islogical)
p.addOptional('TimeOffset', 0, @(x)validateattributes(x, {'numeric'}, ...
{'scalar', 'real', 'positive'}))
% Parse and validate all input arguments.
p.StructExpand = true;
p.KeepUnmatched = true
p.parse(edot, time, varargin{:})
p.Results
Answers (0)
Categories
Find more on Argument Definitions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!