"Input parser" vs. new "arguments" definition

R2019b introduced the new "arguments" section for function argument checking and parsing.
I hahe written some functions using the "old" input parser class. Are there any advantages to switch to the new syntax?
Is it worth to update my functions to the new syntax? (sure, I know, "never change a running system")
regards, Stefan

1 comentario

chicken vector
chicken vector el 7 de Abr. de 2023
Hi Stefan, this is a question of great interest.
Have you figured out an answer yet? I am implementing a library and I can't choose what to use.

Iniciar sesión para comentar.

Respuestas (1)

埃博拉酱
埃博拉酱 el 7 de Abr. de 2023
Editada: 埃博拉酱 el 7 de Abr. de 2023

2 votos

The biggest advantage of the arguments block is readability, but it is far less flexible than varargin. People who are not familiar with your code can quickly understand your parameter requirements through the arguments block.
Another potential advantage may be ease of compiler optimization. Overly flexible varargin is difficult to optimize for performance.

4 comentarios

Thank you for your answer.
I am new to using OOP in Matlab so I have been experimenting a bit with both and I would add that a limitation of using arguments over inputParser and varargin is the impossibility of using a single option input like:
options = {"parameters1", 1, "parameter2", 'foo', "parameter3", true}
myfunction(requiredInput, options)
When you use arguments you are obligated to write instead:
myfunction(requiredInput, "parameters1", 1, "parameter2", 'foo', "parameter3", true)
This can be a little boring when you have to pass options between sub/superclasses.
Nevertheless, I could be wrong about this due to my inexperience.
埃博拉酱
埃博拉酱 el 7 de Abr. de 2023
From your description, I don't quite understand what you're trying to do.
Simon
Simon el 26 de Jun. de 2023
I have the same question. Both inputParser and arguments are unfamiliar to me.
Stephen23
Stephen23 el 26 de Jun. de 2023
Editada: Stephen23 el 26 de Jun. de 2023
@chicken vector: you can always use a comma-separated list, which works on every MATLAB version**:
options = {"parameters1", 1, "parameter2", 'foo', "parameter3", true}
myfunction(requiredInput, options{:})
** with cell arrays. AFAIK comma-separated lists were introduced at the same time.

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 17 de En. de 2022

Editada:

el 26 de Jun. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by