フィルターのクリア

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 Center および File ExchangeGet Started with MATLAB Compiler SDK についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by