(problem solved, question revised) How to select table rows with varargin and not using logical condition as input argument.
Show older comments
(original question)"I have tables. Selecting rows where values meet logical conditions from them is a frequent task. It becomes tiresome when it's done over many tables and logical conditions, and error-prone. So I try to make it easier that I don't need to type repetitive logical conditions. My current solution is using inputParser to re-syntax it as name-value argument.
Because this is the first time I use inputParser, my solution is done in an ad-hoc manner. Any suggestons for improvement is greatly appereciated. For example, in addParameters( ) how do I handle the default values, which I don't need to specify at all."
(revised question) My original question did not really ask what I want to ask. The more accurately way to state my question is: Is there any way to set default input argument to be of all possible values. So when an input argument is not specified, all possible values will be included in a selection logic?
Demo data
T = cell2table({'income', 2020, 1; 'income', 2020, 2; 'income', 2021, 1; 'income', 2021, 2; ...
'expense', 2020, 1; 'expense', 2020, 2}, "VariableNames", {'account', 'year', 'month'});
The output I want for my codes to achieve but fail.
ix = T.account=="income";
% when year and month are not specified in the selection,
% all years and months will be included.
T(ix, :)
% when account and year are assigned values but month is not,
% all months will be included in the selection logic.
ix = T.account=="income";
iy = T.year==2020;
T(ix&iy,:)
However, my codes fail to get similar outputs.
foo(T, 'account', 'expense')
% I want all years and months included in the output.
My codes
function out = foo(T, varargin)
% ???? How to set default values to be all possible values ????%
defaultAccount='income';
defaultYear = 2020;
defaultMonth = 1;
p = inputParser;
addRequired(p, 'T');
addParameter(p, 'account', defaultAccount);
addParameter(p, 'year', defaultYear);
addParameter(p, 'month', defaultMonth);
parse(p, T, varargin{:});
if ismember('account', p.UsingDefaults)
ixAccount = true;
else
ixAccount = T.account==string(p.Results.account);
end
if ismember('year', p.UsingDefaults)
ixYear = true;
else
ixYear = T.year==p.Results.year;
end
if ismember('month', p.UsingDefaults)
ixMonth = true;
else
ixMonth = T.month==p.Results.month;
end
% ???? When year or month is not specified, how to
% make ixYear or ixMonth not appear in the final
% selection logic or they would be set 'true'????
ixRows = ixAccount&ixYear&ixMonth;
out = T(ixRows, :);
end
Accepted Answer
More Answers (2)
Satwik
on 26 Jun 2023
0 votes
Hi Simon,
I understand that you are using inputParser in your code but don't want to specify the default values as there is no logic associated to the default values.
The inputParser function requires that a default value be provided for each parameter in case the user does not pass in a value for that parameter. This is to ensure that the function can be parsed correctly even if the user does not pass in all of the required parameters.
Hope this helps!
2 Comments
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!