🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
inputParser - Input parser for functions - MATLAB
The inputParser object enables you to manage inputs to a function by creating an input parser scheme.
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
Parse Function Inputs - MATLAB & Simulink
Define required and optional inputs, assign defaults to optional inputs, and validate all inputs to a custom function using the Input Parser.
Discussions

Simple Explanation for ‘parse’
If using a recent version of MATLAB, function argument validation is massively superior to the inputParser. It’s also conceptually easier to understand I think. https://www.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html#mw_b1fb2665-7be4-4fa5-a076-f82ad44304f4 If you are asking because you’re looking at existing code. The inputParser is an object that you configure to describe required, optional and Name/Value parameters that a function accepts. Once configured, you call the parse function along with the arguments to your function and the Results property of the parser returns arguments as a struct. That’s the idea. If writing new code with modern MATLAB, prefer function argument validation. It comes with automatic tab completion as an added perk. More on reddit.com
🌐 r/matlab
5
17
April 9, 2020
How do I parse two input arguments for matching dimensions with Matlab Input Parser?
How do I integrate tests spanning more than one variable (i.e. the if-statement in Line B) in the concept of the Matlab Inputparser Class? More on mathworks.com
🌐 mathworks.com
1
1
July 25, 2014
How do I use MATLAB's inputParser with optional string inputs? The documentation says "use a validation function" but it's unclear how to do that - Stack Overflow
I have a MATLAB file that contains a single top-level function, called sandbox. That function in turn contains two nested functions, mysum and myprod, which are identical in functionality and what More on stackoverflow.com
🌐 stackoverflow.com
Input Parser and Enumerations
Can anyone help me with why this might not work? classdef WhichIlsop %% enumeration Prod() Test() end end Then when this enum is p... More on mathworks.com
🌐 mathworks.com
1
0
October 8, 2018
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
Input Parser Validation Functions - MATLAB & Simulink
Use an existing MATLAB® function such as ischar or isnumeric. For example, check that a required input named num is numeric: p = inputParser; checknum = @isnumeric; addRequired(p,'num',checknum) parse(p,'text') The value of 'num' is invalid. It must satisfy the function: isnumeric.
🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
addParamValue - (Not recommended) Add optional name-value pair argument into input parser scheme - MATLAB
p = inputParser; paramName = 'myParam'; defaultVal = 1; errorMsg = 'Value must be positive, scalar, and numeric.'; validationFcn = @(x) assert(isnumeric(x) && isscalar(x) ...
🌐
MathWorks
blogs.mathworks.com › community › 2012 › 02 › 13 › parsing-inputs
Parsing Inputs » MATLAB Community - MATLAB & Simulink
February 13, 2012 - Many MATLAB functions take multiple optional arguments, or arguments specified as param-value pairs. A param-value pair is usually supplied with a string parameter name, such as ‘Position’ or ‘Color’ followed by the value of that parameter such as [0 0 1 1] or ‘on’ or some such. You may have seen this paradigm with the graphics functions. With the help of the inputParser class, it’s easy to add param-value pairs to your own functions.
Find elsewhere
🌐
MathWorks
blogs.mathworks.com › videos › 2018 › 12 › 17 › trying-out-inputparser
Trying Out inputparser » Stuart’s MATLAB Videos - MATLAB & Simulink
December 17, 2018 - Currently I have a function which already has a few parameters, and I want to add more. With positional parameters, you have to supply all the ones up to the one you need which can get cumbersome if there are many. So I thought I would try using inputparser and make some of the parameters name-value pairs.
Top answer
1 of 2
5

The problem is that validatestring returns the matching string from the cell argument ({'imag',''}) rather than a Boolean indicating if it passes validation. Instead, use strcmp and any:

@(x) any(strcmp(x,{'imag', ''}))

Also, with validatestring, if the input string did not match either 'imag' or '' (actually just 'imag' since empty strings only match in R2014a+), it would throw an error rather than returning false so that the inputParser could return the appropriate error.

Another nice way to fix the problem is to change the syntax of applyFunc entirely so that instead of just 'imag' as an optional string input argument, use a Parameter-Value with 'imag' as the parameter and a validated boolean as the input.

The input definition suggested by Amro in the comments:

p.addParameter('imag', false, @(x)validateattributes(x, {'logical'}, {'scalar'}))

The usage:

mysum(x,'imag',true)
mysum(x)               % default is equivalent to mysum(x,'imag',false)

This would simplify the rest of the code with p.Result.imag being a logical scalar. I would suggest:

x = f(v) + p.Result.imag*1i;
2 of 2
2

The problem is not inputParser, I think the issue is with validatestring.

1) First it does not match on empty strings:

>> x = ''
x =
     ''

>> validatestring(x, {'imag',''})
Expected input to match one of these strings:

imag,

The input did not match any of the valid strings.
Caused by:
    Error using validatestring>checkString (line 85)
    Expected input to be a row vector. 

2) Second, if it successfully matches, it returns the resolved string (from one of the valid choice), instead of true/false. inputParser requires that the validation function either return a boolean, or nothing but throws error on failure.

🌐
MathWorks
mathworks.com › matlabcentral › answers › 422774-input-parser-and-enumerations
Input Parser and Enumerations - MATLAB Answers - MATLAB Central
October 8, 2018 - https://www.mathworks.com/matlabcentral/answers/422774-input-parser-and-enumerations · Cancel Copy to Clipboard · Can anyone help me with why this might not work? classdef WhichIlsop · enumeration · Prod() Test() end · end · Then when this enum is passed into a function with parameter name ilsop, I want to validate the inputs. p = inputParser; p.KeepUnmatched = true; p.FunctionName = 'bah'; addRequired(p, 'ilsop', @(x) isa(x, 'WhichIlsop')); p.parse(varargin{:}); I get this message when evaluating the 'parse' line.
🌐
Medium
medium.com › @jayw711kb › how-to-parse-input-in-matlab-a94e8d43ce2d
How to parse input in MatLab? - Jay Huang - Medium
February 3, 2022 - How to parse input in MatLab? How to parse input in MatLab? [ans] inputParser [description] (1)inputparser: an input parser for functions. (2)parse: given a variable to test if it can be parsed with …
🌐
Mit
lost-contact.mit.edu › afs › inf.ed.ac.uk › group › teaching › matlab-help › R2016b › matlab › ref › inputparser-class.html
inputParser class
The inputParser object allows you to manage inputs to a function by creating an input scheme. To check the input, you can define validation functions for required arguments, optional arguments, and name-value pair arguments.
🌐
Substack
lonelyneuron.substack.com › the lonely neuron › checking your ins and outs: the underused matlab input parser
Checking your Ins and Outs: The underused MATLAB Input Parser
April 30, 2022 - I want the user to input an EEGLAB EEG SET structure and some other parameters that I will define as I write the function. We specify the “other inputs” to MATLAB using the varargin command. function mvar = fx_gedcfc_base( EEG, varargin ) end · Next, we will create an inputParser object which we will call p.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1874827-how-to-find-required-inputs-from-input-parser-object
How to find required inputs from Input Parser object? - MATLAB Answers - MATLAB Central
December 9, 2022 - Hi Community, I am have implemented 'InputParser' for my MATLAB function. Here's the code snippet- function fitElliOntoDatam_10_edit(filen,NrPlanes,makePlots) defaultMakePlot = false; %%%%%...