# Can Matlab's arguments function handle structs

62 views (last 30 days)
Clownfish on 25 Apr 2020
Commented: Sriram Tadavarty on 7 May 2021 at 10:03
Hi,
I was wondering if it is possible to do argument validation with Matlab's arguments function like in the following example
function foo(Struct)
arguments
Struct.A double = 1
Struct.B double = 2
Struct.C char = 'hello
end
...
end

Sriram Tadavarty on 25 Apr 2020
Edited: Sriram Tadavarty on 7 May 2021 at 10:02
Hi Clownfish,
The function validation with input structure is not possible directly, instead, you can pass the structure as name value pairs to the function. Look at the Name-Value pair section here.
The valid function calls to validate the function posted in the question are:
foo("A",100,"B",25,"C","Arguments")
foo('A',100,'B',25,'C','Arguments')
% From R2021a onwards, the following are also possible
foo(A = 100, B = 25, C = "Arguments")
foo(A = 100, C = 'Arguments')
For the script you have provided, you can do slight modifications to check if it works, as shown below:
function [a,b,c] = foo(Struct)
arguments
Struct.A double = 1
Struct.B double = 2
Struct.C char = 'hello'
end
a = Struct.A;
b = Struct.B;
c = Struct.C;
end
>> foo % outputs 1
>> [a,b,c] = foo % Outputs a with value 1, b with value 2 ,and c with value 'hello'
>> [a,b,c] = foo("A",100,"B",25,"C","Arguments") % Outputs a with 100, b with 25, and c with 'Arguments'
Hope this helps.
Regards,
Sriram
PS: Updated the answer to make it clear. See the comments for more details.
Sriram Tadavarty on 7 May 2021 at 4:43
The suggestion in the answer was to use the fields of the structure as name value pairs to function rather than the structure itself. Since, structure is not accepted directly to perform validation. It can't be used directly as inputParser.
[a,b,c] = foo(struct("A",100,"B",25,"C","Arguments"))
The suggestion was to perform the below:
foo("A",100,"B",25,"C","Arguments")
Observe the possible ways of calling the function having structure input argument with the example shown here. Though you have structure input, you cannot pass it as structure directly, rather you can pass them as name,value arguments.
For example, you can add validation function to fields and check if that works or not:
function [a,b,c] = foo(Struct)
arguments
Struct.A double {mustBePositive} = 1
Struct.B double {mustBeNonnegative} = 2
Struct.C char = 'hello'
end
a = Struct.A;
b = Struct.B;
c = Struct.C;
end
Now try this,
>> [a,b,c] = foo("A",0,"B",-1,"C","Arguments"); % You get the below error
Error using foo
Invalid name-value argument 'A'. Value must be positive.
>> [a,b,c] = foo("A",1,"B",-1,"C","Arguments"); % You get the below error
Error using foo
Invalid name-value argument 'B'. Value must be nonnegative.
Hope this helps.
Regards,
Sriram

Walter Roberson on 7 May 2021 at 9:48
It does not appear to be possible.
One work-around would be
temp = [string(fieldnames(Struct)), struct2cell(Struct)].';
foo(temp{:});
after which, as @Sriram Tadavarty shows, you can validate in the form of a structure. Their answer shows what is actually possible; I am only adding this because of complaints that there needs to be an explicit "No" for the question as asked
Sriram Tadavarty on 7 May 2021 at 10:03
Thanks for this @Walter Roberson

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by