Is it possible to pass argument blocks between functions?

5 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-8-23
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 2023-8-23
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.

请先登录,再进行评论。

回答(1 个)

Gagan
Gagan 2023-9-4
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 个评论
jr1995
jr1995 2023-9-4
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by