Skip to content

treefun* functions support variable output arguments #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions @tree/treefun.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
function newTree = treefun(obj, fun)
function varargout = treefun(obj, fun)
%% TREEFUN Create a new tree by applying function handle to each node of the source tree.

if ~isa(fun, 'function_handle')
error('MATLAB:tree:treefun', ...
'Second argument, fun, must be a function handle. Got a %s.', ...
class(fun));
end

% First we copy
newTree = tree(obj, 'clear');

% Then we override
newTree.Node = cellfun(fun, obj.Node, 'UniformOutput', false);

% validate number of output arguments is <= # outputs of `fun`
nargoutchk(0,abs(nargout(fun)));

% if called interactively, nargout=0, which causes problems in `cellfun`
% given:
% >> t
% t = ...
% Node: {3x1 cell}
% e.g. if this isn't done...
% >> t.treefun(@(x) x+1)
% >>
% but by setting output number to 1, `ans` will be assigned
% >> t.treefun(@(x) x+1);
% ans = ...
% Node: {3x1 cell}
if ~nargout
n_out = 1; % need new variable b/c can't
% reassign nargout
else
n_out = nargout;
end

% cellfun call collects output into n_outx1 cell array
[nodes{1:n_out}] = cellfun(fun,obj.Node,'UniformOutput',false);
for k = 1:n_out % copy each output to its own tree
varargout{k} = tree(obj,'clear');
varargout{k}.Node = nodes{k};
end
end
35 changes: 24 additions & 11 deletions @tree/treefun2.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
function obj = treefun2(obj, val, fun)
function varargout = treefun2(obj, val, fun)
%%TREEFUN2 Two-arguments function on trees, with scalar expansion.
if ~isa(fun, 'function_handle')
error('MATLAB:tree:treefun', ...
'Second argument, fun, must be a function handle. Got a %s.', ...
class(fun));
end
nargoutchk(0,abs(nargout(fun))); % validate # output args

[obj, val] = permuteIfNeeded(obj, val);

if isa(val, 'tree')

content = cellfun(fun, obj.Node, val.Node, ...
'UniformOutput', false);
% if nargout = 0, set it to 1 so that `ans` is assigned
% (see comment in `treefun.m`)
if ~nargout, n_out = 1; else n_out = nargout; end

if isa(val, 'tree')

else

content = cellfun(@(x) fun(x, val), obj.Node, ...
'UniformOutput', false);
[content{1:n_out}] = cellfun(fun, obj.Node, val.Node,...
'UniformOutput',false);

end
else

[content{1:n_out}] = cellfun(@(x) fun(x, val), obj.Node, ...
'UniformOutput', false);

end

obj.Node = content;
for k = 1:n_out
varargout{k} = tree(obj,'clear');
varargout{k}.Node = content{k};
end
end