Effacer les filtres
Effacer les filtres

Get function argument name and type

9 vues (au cours des 30 derniers jours)
Marleen
Marleen le 25 Jan 2022
Commenté : Loïc Reymond le 3 Août 2022
Hi,
With the new addition of function argument validation [Link] using the arguments keyword, is there a (preferably built-in) way to retrieve the argument names and types?
So say you have a function
function MyFunc(a, b)
arguments
a uint32 {mustBeInRange(a,0,5)} = 2;
b string = "Default String";
end
...
And say this builtin function is called arginfo or argspec (like python's getfullargspec [Link])
>> arginfo('MyFunc')
I'd expect it to return a struct array like this:
ans =
1x2 struct array with fields:
name
size
type
validator
default
size of which corresponds to each argument in the function, in order
>> ans(1)
ans =
struct with fields:
name: 'a'
size [] % ?
type: 'uint32'
validator @()mustBeInRange(a,0,5) % ?
default: 2;
This is of course possible to do when you have access to the source file. Just read the file and parse the code between arguments. But what if you compile it using mcc? Then there's no way to retrieve the source code.
I am not sure how to handle repeating and named arguments.
--
This would be a nice function to have. Use case: I am making a GUI which basically creates user input based on the function inputs. Bool -> uicheckbox. String -> uieditfield. double -> numeric uieditfield. Filepath (string with validator mustBeFolder) -> custom component containing uieditfield and a browse button.
Kind regards
  1 commentaire
Loïc Reymond
Loïc Reymond le 3 Août 2022
That would be a very useful function indeed! If anyone has a solution, it'd be great!

Connectez-vous pour commenter.

Réponses (1)

Benjamin Thompson
Benjamin Thompson le 25 Jan 2022
See the article "Function Argument Validation" in the MATLAB help. MATLAB will silently convert for you if possible, so you may not want to try to improve on that. My impression is that this new feature makes MATLAB functions a little bit more explicitly typed like other programming languages, because otherwise the function argument list does not include any type information andn the default type was double.
  1 commentaire
Marleen
Marleen le 26 Jan 2022
I have linked said article in the question, and I didn't ask how the function argument validation works or how to use it.
I asked how to retrieve the argument names and (preffered) types of a function which uses this arguments keyword: basically if there's a matlab equivalent of python's getfullargspec [Link].

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB Compiler SDK dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by