Filter löschen
Filter löschen

Get function argument name and type

9 Ansichten (letzte 30 Tage)
Marleen
Marleen am 25 Jan. 2022
Kommentiert: Loïc Reymond am 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 Kommentar
Loïc Reymond
Loïc Reymond am 3 Aug. 2022
That would be a very useful function indeed! If anyone has a solution, it'd be great!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Benjamin Thompson
Benjamin Thompson am 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 Kommentar
Marleen
Marleen am 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].

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB Compiler SDK finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by