Borrar filtros
Borrar filtros

Can Matlab's arguments function handle structs

191 visualizaciones (últimos 30 días)
Clownfish
Clownfish el 25 de Abr. de 2020
Comentada: Stephen23 el 28 de Mzo. de 2024
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

Respuestas (2)

Sriram Tadavarty
Sriram Tadavarty el 25 de Abr. de 2020
Editada: Sriram Tadavarty el 7 de Mayo de 2021
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.
  7 comentarios
Antoine Baudoin
Antoine Baudoin el 28 de Oct. de 2022
Thanks Sriram for the answer.
I am interested in this question because arguments is a great fearture, but it will kill back-compatibility for my already written functions, which are written to be called with struct options.
Is Mathworks working on the solution here?
Markus Leuthold
Markus Leuthold el 4 de En. de 2023
Thanks @Sriram Tadavarty for the answer. As already mentioned before, this is an issue if you want to replace the old inputParser (which allowed struct expansion) with the new arguments validation. Please allow the following syntax
arguments(StructExpand=true)
...
end
which allows to call either
foo("A", 100)
or
s.A = 100
foo(s)
This would ensure compatibility with the old inputParser

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 7 de Mayo de 2021
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
  4 comentarios
Jeremy Berke
Jeremy Berke el 28 de Mzo. de 2024
@Walter Roberson I think this solution runs into a problem is the values are not strings. In the code below the value should be 2, but it is instead "2" so when I have a validator value (1,1) {mustBeNumeric} it will throw an error because "2" is a string.
Perhaps using value (1,1) double will cast the string into a double, but that is kind of defeating the purpose of the input validator
y.name = "b"; y.value = 2;
nvp = [string(fieldnames(y)) struct2cell(y)].'
nvp = 2×2 string array
"name" "value" "b" "2"
nvp{:}
ans = 'name'
ans = 'b'
ans = 'value'
ans = '2'
Stephen23
Stephen23 el 28 de Mzo. de 2024
@Jeremy Berke: STRING is not required, you can just use the cell array instead:
y.name = "b";
y.value = 2;
y.hello = sqrt(2);
y.world = {'cat','hat'};
c = [fieldnames(y),struct2cell(y)].'
c = 2x4 cell array
{'name'} {'value'} {'hello' } {'world' } {["b" ]} {[ 2]} {[1.4142]} {1x2 cell}
c{:}
ans = 'name'
ans = "b"
ans = 'value'
ans = 2
ans = 'hello'
ans = 1.4142
ans = 'world'
ans = 1x2 cell array
{'cat'} {'hat'}

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by