Evolution of arguments-block functionality
Steve Eddins
on 2 Dec 2024
I wish I knew more about the intended evolution of the capabilities of the function arguments block. I love implementing function syntaxes using this relatively new form, but it doesn't yet handle some function syntax design patterns that I think are valuable and worth keeping.
For example, some functions take an input quantity that can something numeric, or it can be an option string that descriptively names a particular value of that quantity. One example is dateshift(t,"dayofweek",dow), where dow can be an integer from 1 to 7, or it can be one of the option strings "weekday" or "weekend".
Another example is Image Processing Toolbox that take a connectivity specifier as input. The function bwconncomp is one particular case. Connectivity can be specified using certain scalars, certain arrays, or the option string "maximal".
I think this is a worthwhile function design pattern, but I don't think the arguments block validation functionality supports it well (unless you use a lot of extra code that duplicates standard MATLAB behavior, which undermines the value of the arguments block).
MathWorkers - believe me, I know that it is not in your DNA to discuss future features. But would anyone care to offer a hint about directions for the arguments block functionality?
8 Comments
That is a great article, Steve. I posted a comment there, but cannot see how to format code in the comments on that site. So, I have included suggested code here, as well, in an easy-to-read format:
function t2 = dayofweek_shift_v4(t, dow)
arguments
t datetime
dow {custom_dow_cheker(dow)} = 1
end
t2 = dateshift(t, "dayofweek", dow);
end
function custom_dow_cheker(dow)
if isnumeric(dow)
mustBeScalarOrEmpty(dow);
mustBeInteger(dow);
mustBeInRange(dow, 1, 7);
else
mustBeTextScalar(dow);
mustBeMember(dow, ["weekday", "weekend"]);
end
end
I spent some time last week updating some of my very old File Exchange submissions. It was a real treat to replace tons of messy complex input arg parsing with function argument validation, but I got stuck on my functions that use the standard graphics pattern of allowing an optional leading input argument to specify what axes to operate on. I've been trying to decide if I should update those functions to make axes a named argument or if I should push to support this with arguments validation. I think your specific request is better motivated and more likely to be achievable without disrupting the current design.