Code covered by the BSD License  

Highlights from
Clean up context of anonymous function handles - bug #328733

from Clean up context of anonymous function handles - bug #328733 by Volkmar Glauche
This submission provides a workaround for MATLAB bug #328733.

clean_funhandle(fhin)
function fh = clean_funhandle(fhin)
% Create a function handle without unwanted context variables
% FH = CLEAN_FUNHANDLE(FHIN)
% This function tries to provide a workaround for MATLAB bug #328733. It
% uses FUNC2STR to create a string representation of the input function
% handle FHIN. This function handle is cleared and EVAL is used to create
% the new function handle.
%
% See also http://www.mathworks.com bug #328733, FUNCTION_HANDLE,
% FUNC2STR, EVAL.
%
% This code has been developed as part of a batch job configuration
% system for MATLAB. See  
%      http://sourceforge.net/projects/matlabbatch
% for details about the original project.
%_______________________________________________________________________
% Copyright (C) 2007 Freiburg Brain Imaging

% Volkmar Glauche
% $Id: gencode.m 410 2009-06-23 11:47:26Z glauche $

rev = '$Rev: 410 $'; %#ok

if isa(fhin, 'function_handle')
    if isempty(fhin)
        try
            % create new empty function handle
            % might not work in pre-2008a releases
            fh = function_handle.empty;
        catch
            % pass input to output
            fh = fhin;
        end
    else
        % create string representation of input handle
        fstr = func2str(fhin);
        if fstr(1) == '@' % only anonymous function handles start with an @
            % paranoia - don't keep anything that might confuse MATLAB when
            % creating the new function handle
            clear fhin;
            % create new function handle
            eval(['fh = ' fstr ';']);
        else
            % simply pass input to output - bug does not exist for simple
            % function handles
            fh = fhin;
        end
    end
else
    % fhin is not a function handle, pass it on unchanged
    fh = fhin;
end

Contact us at files@mathworks.com