Clear Filters
Clear Filters

Get function argument name and type

9 views (last 30 days)
Marleen
Marleen on 25 Jan 2022
Commented: Loïc Reymond on 3 Aug 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 Comment
Loïc Reymond
Loïc Reymond on 3 Aug 2022
That would be a very useful function indeed! If anyone has a solution, it'd be great!

Sign in to comment.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 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 Comment
Marleen
Marleen on 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].

Sign in to comment.

Categories

Find more on Get Started with MATLAB Compiler SDK in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!