query argument block options

4 visualizaciones (últimos 30 días)
Steve Van Hooser
Steve Van Hooser el 26 de Mzo. de 2025
Editada: Sameer el 9 de Abr. de 2025
Is there a way to automatically query the optional arguments of an argument block?
For example, something like:
myStruct = argumentInfo('myfunc.m');
(and myStruct has fields 'name','type','size','validationCode','defaultValue')
  1 comentario
Walter Roberson
Walter Roberson el 26 de Mzo. de 2025
I do not think any such thing existed.
If it did exist, it would need to also accept a function handle instead of a file name, to deal with the fact that there can be multiple functions in a file. Or I suppose it could return a struct array of struct array, with the outer layer being information about the function name.

Iniciar sesión para comentar.

Respuestas (1)

Sameer
Sameer el 9 de Abr. de 2025
Editada: Sameer el 9 de Abr. de 2025
You can use "inputParser" class to define and query optional arguments of a function
Here's an example:
function Ex_Func(varargin)
p = inputParser;
% Define optional arguments with default values and validation functions
addOptional(p, 'name', 'defaultName', @ischar);
addOptional(p, 'type', 'defaultType', @ischar);
addOptional(p, 'size', [1, 1], @isnumeric);
addOptional(p, 'validationCode', 'defaultValidation', @ischar);
addOptional(p, 'defaultValue', 'defaultValue', @ischar);
% Parse the input arguments
parse(p, varargin{:});
% Retrieve the parsed arguments
myStruct = struct();
myStruct.name = p.Results.name;
myStruct.type = p.Results.type;
myStruct.size = p.Results.size;
myStruct.validationCode = p.Results.validationCode;
myStruct.defaultValue = p.Results.defaultValue;
disp(myStruct);
end
Hope this helps!

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by