Is it possible to pass argument blocks between functions?

5 views (last 30 days)
Hello,
I was wondering if it is possible to reuse argument block of functions. Maybe the following example makes my idea more clear:
In a data class I have a filter method. This filter method has an argument block with multiple filter criteria:
function output = findTest(obj, filterlogic_in, options)
arguments
obj className
filterlogic_in {mustBeMember(filterlogic_in,{'and', 'or', 'not'})} = 'and'
options.filter1 (1,:) = nan
options.filter2 (1,:) {mustBeMember(options.filter2,{'', 'test1', 'test2'})} = {''} %Filter for test type
end
%here happens the filtering
end
In the class I also have multiple methods that do a filtering first if needed, e.g. calculate average data:
function output = calcAvg(obj, filterlogic_in, options)
arguments
obj className
filterlogic_in {mustBeMember(filterlogic_in,{'and', 'or', 'not'})} = 'and'
options.filter1 (1,:) = nan
options.filter2 (1,:) {mustBeMember(options.filter2,{'', 'test1', 'test2'})} = {''} %Filter for test type
end
%Use the findTest method
filteredData = obj.findTest(filterlogic_in, "filter1", options.filter1, "filter2", options.filter2)
%here starts the rest of the function to calculate the average
end
My question: Is it possible to 'pass' or 'inhert' the argument block from the findTest method to other methods. E.g. If I add a new filter, then I have to change the arguement block of all other methods using findTest.
Thanks a lot
  2 Comments
Stephen23
Stephen23 on 23 Aug 2023
Why does CALCAVG to check the arguments for FINDTEST? Let each function determine its own input requirements, i.e. simply pass the inputs to FINDTEST, and let FINDTEST decide if they are okay or not.
jr1995
jr1995 on 23 Aug 2023
I would like to have the option to use the tabulator key during the input, to display the possible options for filtering in the calcavg method as well.

Sign in to comment.

Answers (1)

Gagan
Gagan on 4 Sep 2023
Hi jr1995
Reusing the arguments block in multiple functions is indeed possible. One approach is to create a dedicated function that handles argument validation using the arguments’ block, and the other functions can invoke this function to validate the arguments. This way, the validation logic is centralized and can be reused across multiple functions.
For Example :-
function argumentsBlock(className, filterlogic_in, options)
arguments
className
filterlogic_in {mustBeMember(filterlogic_in,{'and', 'or', 'not'})} = 'and'
options.filter1 = nan
options.filter2{mustBeMember(options.filter2,{'', 'test1', 'test2'})} = {''}
end
end
function output = findTest()
options.filter1 = nan;
options.filter2 = "test1";
argumentsBlock("Hello",'and', "filter1", options.filter1, "filter2", options.filter2);
end
In the above code snippet,
Function named 'argumentBlock' is responsible for validating the arguments. And the function 'findTest' utilizes the 'argumentBlock' function to perform the necessary argument validation.
  1 Comment
jr1995
jr1995 on 4 Sep 2023
Hi,
I found another solution for my problem. I defined a class for my filter criteria:
classdef class_Filter_v01
properties
filterLogic {mustBeMember(filterLogic,{'and', 'or', 'not'})} = 'and'
filter1 (1,:) = nan
end
end
Than I call the properties in the argument block of my function. For filter criteria that are not set during the function call, I get the default property values from the class and add them to the structure filterArgs.
function output = findTest(obj, filterArgs)
arguments
obj
filterArgs.?class_Filter_v01
end
%If properties are not set, add default values from class
mco = metaclass(class_Filter_v01);
for ii=1:size(mco.PropertyList, 1)
if isfield(filterArgs, mco.PropertyList(ii).Name) == false
filterArgs.(mco.PropertyList(ii).Name) = mco.PropertyList(ii).DefaultValue;
end
end
% here is the filtering
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!