필터 지우기
필터 지우기

Get function argument name and type

조회 수: 9 (최근 30일)
Marleen
Marleen 2022년 1월 25일
댓글: Loïc Reymond 2022년 8월 3일
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
Loïc Reymond
Loïc Reymond 2022년 8월 3일
That would be a very useful function indeed! If anyone has a solution, it'd be great!

댓글을 달려면 로그인하십시오.

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 1월 25일
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
Marleen
Marleen 2022년 1월 26일
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].

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB Compiler SDK에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by