Skip to content

Commit

Permalink
Create spm_subfun.m
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAshburner authored Sep 9, 2020
1 parent 694e424 commit 0fd87d8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions spm_subfun.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function varargout = spm_subfun(varargin)
% Enable calling local functions
% FORMAT [o1,o2,...] = spm_subfun(localfunctions,action,i1,i2,...)
% The funcion is supposed to be inserted into multifunction m-files
% so that it calls localfunctions within the scope of the m-file.
% The output of this is used to match the action string with the
% name of each local function to see which of them to call.
%__________________________________________________________________________
% Copyright (C) 2020 Wellcome Centre for Human Neuroimaging

% $Id$
if nargin<=1
[varargout{1:nargout}] = import(varargin{1:nargin});
else
[varargout{1:nargout}] = select(varargin{1:nargin});
end
%==========================================================================

%==========================================================================
function varargout = select(funs,opt,varargin)
opt = lower(opt);
s = import(funs);
if ~isfield(s,opt), error('Unknown function (%s)',opt); end
[varargout{1:nargout}] = feval(s.(opt),varargin{:});
%==========================================================================

%==========================================================================
function varargout = import(funs,varargin)
names = cellfun(@(x)lower(char(x)),funs,'UniformOutput',false);
c = {names{:}; funs{:}};
varargout{1} = struct(c{:});
%==========================================================================

0 comments on commit 0fd87d8

Please sign in to comment.