parseargs: Simplifies input processing for functions with multiple options
Based on same idea as the excellent "parse_pv_pairs", and similar to the "inputParser" object added to MATLAB in R2007a, this function handles names and values for properties in any order, and assigns the values to a structure containing default values.
For example, in your function which has options "StartTime" and
"StopOnError", create a structure of defaults and pass inputs to parseargs:
| function myfunction(varargin)
| X.StartTime = 0;
| X.StopOnError = true;
| X = parseargs(X,varargin{:})
If the user specifies inputs:
| myfunction('StopOnError',false,'StartTime',5)
the structure will be modified accordingly.
The aim of this function is to ease as much as possible the tedious and error prone task of handling inputs, specifically with a view to application development (rather than quick scripting), where robustness is of high importance.
To this end, the function enforces some rules, partly to reduce the chance of mistakes by the caller, and partly to save the user from writing additional code to enforce them:
1) Property names must be specified in full, and are case-sensitive
2) The data type of a property cannot be changed (unless the property was initially empty)
3) Scalar values must remain scalar
4) Where the original value is a cell array it must contain strings. The new value must be one of these strings, and if no new value is specified, the first string is selected as a default.
Zitieren als
Malcolm Wood (2025). parseargs: Simplifies input processing for functions with multiple options (https://www.mathworks.com/matlabcentral/fileexchange/10670-parseargs-simplifies-input-processing-for-functions-with-multiple-options), MATLAB Central File Exchange. Abgerufen.
Kompatibilität der MATLAB-Version
Plattform-Kompatibilität
Windows macOS LinuxKategorien
- MATLAB > Programming > Functions > Argument Definitions >
- MATLAB > Language Fundamentals > Data Types > Characters and Strings > String Parsing >
Tags
Quellenangaben
Inspiriert von: Parse function arguments, parse_pv_pairs
Inspiriert: parseargs - A very flexible inputs parser, Experimental (Semi-) Variogram, InputPreprocessor, easyparse, variogramfit, Continuous Sound and Vibration Analysis
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
html/
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
1.2.0.1 | Updated license |
||
1.2.0.0 | Added copyright lines and suppressed M-Lint warnings. |
||
1.1.0.0 | Review |
||
1.0.0.0 |