Single struct as alternative to name-value pairs in user-defined function
Hello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any helpHello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any help Hello, is it possible when defining a function with name-value argument pairs to allow a user to pass the struct(s) that define these name-value pairs directly to the function?
For example, I can define
function [s1, s2] = TestStructArgs(s1, s2)
arguments
s1 (1,1) struct
s2 (1,1) struct
end
end
then call TestStructArgs(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4))
Or, I can define
function [s1, s2] = TestStructArgs2(s1, s2)
arguments
s1.a (1,1) double
s1.b (1,1) double
s2.x (1,1) double
s2.y (1,1) double
end
end
and call TestStructArgs2(‘a’, 1, ‘b’, 2, ‘x’, 3, ‘y’, 4)
But what if I would also like to call the second example by passing whole structs, TestStructArgs2(struct(‘a’, 1, ‘b’, 2), struct(‘x’, 3, ‘y’, 4)) ? There doesn’t appear to be any ambiguity in allowing both approaches, if they’re not mixed-and-matched.
Further, some MATLAB built-in functions permit this e.g. the following runs just fine
>> opts = struct(‘Color’, ‘r’, ‘LineWidth’, 2);
>> x = linspace(0,1,200); y = cos(x);
>> plot(x, y, opts)
There is an answer on the messageboard from 2022 here which suggests adding the attribute StructExpand after arguments but this causes an error if I add it to the above example
Many thanks for any help arguments, struct, validation, name-value MATLAB Answers — New Questions