🌐
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 to use the "parse" function to create optional function inputs
How to use the "parse" function to... Learn more about parse MATLAB More on mathworks.com
🌐 mathworks.com
2
0
May 2, 2017
is there any way to parse the matlab code ?
Commands or built-in functions to parse the Matlab source-code? More on mathworks.com
🌐 mathworks.com
2
0
November 11, 2018
How to parse text data
How to parse text data . Learn more about timeseries, string MATLAB More on mathworks.com
🌐 mathworks.com
3
0
July 17, 2019
🌐
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. To check the input, you can define validation functions for required arguments, optional arguments, and name-value pair arguments.
🌐
GeeksforGeeks
geeksforgeeks.org › software engineering › parse-function-inputs-in-matlab
Parse Function Inputs in MATLAB - GeeksforGeeks
April 28, 2025 - Parsing allows us to assign the value of a variable or parameter to a variable in the source code or directly use the value itself. In MATLAB, the parse function is used to extract input arguments from arrays of characters or strings.
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 32769-mparser
mparser - File Exchange - MATLAB Central
September 1, 2011 - The goal of this package is to provide a way to parse MATLAB code from within MATLAB, and to be able to do source code translations on the resulting AST.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 323470-how-to-use-the-parse-function-to-create-optional-function-inputs
How to use the "parse" function to create optional function inputs - MATLAB Answers - MATLAB Central
May 2, 2017 - I am trying to use "parse" to create optional function inputs. Tried to replicate examples in the documentation (see below and attached) but I get the "too many arguments" error. Any idea? Thanks ... Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/323470-how-to-use-the-parse-function-to-create-optional-function-inputs#answer_253506
Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 472084-how-to-parse-text-data
How to parse text data - MATLAB Answers - MATLAB Central
July 17, 2019 - Then use the fixedWidthImportOptions version, which is more reliable than the textscan version. Matlab does all the hard work for you.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 497882-parsing-text-files-for-beginners
Parsing text files for Beginners - MATLAB Answers - MATLAB Central
December 24, 2019 - /* Just like any C++, we have the open the text file to use, and fid is the file identifier. However, we do not import the data of the txt file, but we actually add the file in the matlab directory path which can be used. ... /* this is using the textscan function to parse it and feed it to the variable out.
🌐
MathWorks
blogs.mathworks.com › community › 2012 › 02 › 13 › parsing-inputs
Parsing Inputs » MATLAB Community - MATLAB & Simulink
February 13, 2012 - Specify All The Inputs Once you have an input parser object, you need to specify each input that you want to parse. In the follow example I add one required, one optional, and one param-value pair parameter. For each input you have to specify a validation function.
Top answer
1 of 2
5

You can use MATLAB's inputParser class:

http://www.mathworks.com/help/matlab/ref/inputparser-class.html http://blogs.mathworks.com/community/2012/02/13/parsing-inputs/

EDIT: I guess I'll just put the code in as well...

function y = f(x, extraOpts)
    p = inputParser;
    p.addRequired('x', @(x) length(x)>1);
    p.addOptional('visualize', 1, @isscalar);
    p.addOptional('N', 100, @isscalar);
    p.parse(x, extraOpts{:});
    do_things(x, extraOpts);
end

You might need to store the results of the parse, perhaps something along the lines of:

inputs = p.parse(x, extraOpts{:});
do_things(inputs)

END EDIT

Alternatively, you could do something like redefine your function to take the number of values in your struct though it's not as powerful as the input parser solution:

function y = f(N, visualize)
if nargin < 2
  visualize = 0
end
if nargin < 1
  visualize = 0
  N = 100
end
2 of 2
0

I couldn't make the answer work, but I found another solution with inputParser, let me paste here for future reference :

function y = f2(extraOpts)
p = inputParser;
p.addParamValue('par1', 'defaultValue1');
p.addParamValue('par2', def2, @isnumeric);
p.addParamValue('par3', def3, @isnumeric);
p.parse(extraOpts{:});
extraOpts = p.Results;
end

the drawback is that I needed to separate the two inputs x and extraOpts and had to call f2 as a subroutine, defined in the same file as f. Maybe there is a more elegant way for this (i.e. one that does not require another function), but the example I have seen was also doing like that, and since it works, it's good for me :)

🌐
MathWorks
mathworks.com › matlab › programming › functions › argument definitions
addOptional - Add optional, positional argument into input parser scheme - MATLAB
Use addOptional to add an individual argument into the input parser scheme. If you want to parse an optional name-value pair, then use the addParameter function. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 39772-easyparse
easyparse - File Exchange - MATLAB Central
January 15, 2013 - Easy-to-use interface for parsing function inputs in the form of parameter-value pairs.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 196582-best-way-to-parse-text-file
Best way to parse text file - MATLAB Answers - MATLAB Central
April 7, 2015 - https://www.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file#comment_277207 · Cancel Copy to Clipboard · where does BLMA1 belong? It's StnID and not all stations have a StnID value. That makes things a "little" more complicated. the columns of the data block are separated by one or more spaces?