필터 지우기
필터 지우기

Is it possible to pass argument blocks between functions?

조회 수: 7 (최근 30일)
jr1995
jr1995 2023년 8월 23일
댓글: jr1995 2023년 9월 4일
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