Clear Filters
Clear Filters

How to find required inputs from Input Parser object?

5 views (last 30 days)
Hi Community,
I am have implemented 'InputParser' for my MATLAB function. Here's the code snippet-
function fitElliOntoDatam_10_edit(filen,NrPlanes,makePlots)
defaultMakePlot = false;
%%%%%%% INPUT PARSER %%%%%%%%
p = inputParser;
p.FunctionName = 'fitElliOntoDatam_10_edit';
validNum = @(x) isnumeric(x) && (x > 0);
addRequired(p,'filen',@isstring);
addRequired(p,'NrPlanes',validNum);
addOptional(p,'makePlots',defaultMakePlot,@islogical);
parse(p,filen,NrPlanes,makePlots);
disp(p);
.....
......
So, when I print the inputParser object, I get the following detail -
>> fitElliOntoDatam_10_edit("export.txt",20,false)
inputParser with properties:
FunctionName: 'fitElliOntoDatam_10_edit'
CaseSensitive: 0
KeepUnmatched: 0
PartialMatching: 1
StructExpand: 1
Parameters: {'filen' 'makePlots' 'NrPlanes'}
Results: [1×1 struct]
Unmatched: [1×1 struct]
UsingDefaults: {1×0 cell}
How can I display which of these 3 parameters are 'required' or 'optional'? Where is that information stored inside inputParser object?
Also can I display the data type of these parameters from the inputParser object? Where do the validation functions get stored ?
  5 Comments
Atin
Atin on 12 Dec 2022
The intention is to have an XML representation of 'help' option for MATLAB functions, similar to what is being done by xmlhelpy (for Python).
I will look into class metadata and customize code suggestions and completions.
I came across 'mlint' while searching google. Is that something which could be useful?
Atin
Atin on 13 Dec 2022
Edited: Atin on 13 Dec 2022
I tried writing 'functionSignatures.json'. Is there any way to validate the inputs that I manually write in 'functionSignatures.json' ? Even when I write only one input, it gets validated by validateFunctionSignaturesJSON.
{
"_schemaVersion": "1.0.0",
"fitElliOntoDatam_10_edit":
{
"inputs":
[
{"name":"in1", "kind":"required", "type":["file"], "purpose":"Name of file with data"},
{"name":"in2", "kind":"required", "type":["numeric"], "purpose":"Number of point in z-direction"},
{"name":"in3", "kind":"ordered", "type":["logical"], "purpose":"Boolean flag for creating or not creating plots"}
]
}
}
On the other hand, 'mlint' provides a list of all variables in the m-file and 'mtree' provides a M parse tree. Is there a way to exploit these libraries to get 'type' and 'kind' of the variables @Steven Lord?
>> mlint('fitElliOntoDatam_10_edit','-edit')
0 <VOID> -1 E
1 fitElliOntoDatam_10_edit 0 F 1/10
2 filen 1 V
3 NrPlanes 1 V
4 makePlots 1 V
5 defaultMakePlot 1 V
6 false 1 F Amb
7 p 1 V
8 inputParser 1 F Amb
9 validNum 1 V
10 isnumeric 1 F Amb
11 addRequired 1 F Amb
.. ............ . . ....
mtree
1=== * FUNCTION: 1/01
2=== * ETC: 1/10
3=== * ETC: 1/34
4=== * ID: 1/10 (fitElliOntoDatam_10_edit)
5=== * ID: 1/35 (filen)
6=== > ID: 1/41 (NrPlanes)
7=== > ID: 1/50 (makePlots)
8=== * EXPR: 17/17
9=== * EQUALS: 17/17
10=== * ID: 17/01 (defaultMakePlot)
11=== * CALL: 17/19
12=== * ID: 17/19 (false)
13=== > EXPR: 20/03
14=== * EQUALS: 20/03
15=== * ID: 20/01 (p)
16=== * CALL: 20/05
17=== * ID: 20/05 (inputParser)
18=== > EXPR: 21/16
19=== * EQUALS: 21/16
20=== * DOT: 21/02
21=== * ID: 21/01 (p)
22=== * FIELD: 21/03 (FunctionName)
23=== * CHARVECTOR: 21/18 ('fitElliOntoDatam_10_edit')
24=== > EXPR: 22/10
25=== * EQUALS: 22/10
26=== * ID: 22/01 (validNum)
27=== * ANON: 22/12
28=== * ANONID: 22/14 (x)
29=== * ANDAND: 22/30
30=== * CALL: 22/26
31=== * ID: 22/17 (isnumeric)
32=== * ANONID: 22/27 (x)
33=== * PARENS: 22/33
34=== * GT: 22/36
35=== * ANONID: 22/34 (x)
36=== * INT: 22/38 (0)
37=== > EXPR: 24/12
38=== * CALL: 24/12
39=== * ID: 24/01 (addRequired)
40=== * ID: 24/13 (p)
41=== > CHARVECTOR: 24/15 ('filen')
42=== > AT: 24/23
43=== * ID: 24/24 (isstring)
44=== > EXPR: 25/12
45=== * CALL: 25/12
46=== * ID: 25/01 (addRequired)
47=== * ID: 25/13 (p)
48=== > CHARVECTOR: 25/15 ('NrPlanes')
49=== > ID: 25/26 (validNum)
50=== > EXPR: 26/12
51=== * CALL: 26/12
52=== * ID: 26/01 (addOptional)
53=== * ID: 26/13 (p)
54=== > CHARVECTOR: 26/15 ('makePlots')
55=== > ID: 26/27 (defaultMakePlot)
56=== > AT: 26/43
57=== * ID: 26/44 (islogical)
58=== > EXPR: 28/06
59=== * CALL: 28/06
60=== * ID: 28/01 (parse)
61=== * ID: 28/07 (p)
62=== > ID: 28/09 (filen)
63=== > ID: 28/15 (NrPlanes)
64=== > ID: 28/24 (makePlots)

Sign in to comment.

Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!