Code covered by the BSD License  

Highlights from
Define/Undefine Constants

from Define/Undefine Constants by Ruben Faibish
Define and undefine constant variables with no use of global

undefine(varargin)
function undefine(varargin)

% UNDEFINE Undefine constants created by the DEFINE function
%   UNDEFINE('CONSTNAME') undefine a constant named CONSTNAME previously
%   defined with the DEFINE function
%
%   USAGE:
%            UNDEFINE
%            UNDEFINE /? or UNDEFINE -?
%            UNDEFINE('CONSTNAME');
%            UNDEFINE('CONSTNAME1'[,'CONSTNAME2'[,...[,'CONSTNAMEN']...]]);
%
%   UNDEFINE with no argument or UNDEFINE -? or UNDEFINE /? gives this help
%   on the UNDEFINE function
%
%   UNDEFINE('CONSTNAME') or UNDEFINE CONSTNAME undefines the constant
%   CONSTNAME previously defined by the DEFINE function and updates the
%   "definition_list.mat" file
%
%   UNDEFINE('CONSTNAME1'[,'CONSTNAME2'[,...[,'CONSTNAMEN']...]]) or
%   UNDEFINE CONSTNAME1 [CONSTNAME2 [... CONSTNAMEN]]] undefines the
%   constants CONSTNAME1 [, CONSTNAME2 [,...[,CONSTNAMEN]...]]  previously
%   defined by the DEFINE function.
%
%   UNDEFINE all undefines all the constants defined by the DEFINE function
%
% REMARK: The UNDEFINE function deletes m-files only if their H1 line is:
%        "% Function Automatically Generated by Define.m, DO NOT EDIT".
%
% If you try to delete a constant that is not in the definition list,an
% appropriate warning will be disoplayed. In order no to see this kind of
% warning, type: "warning('off','undefine:delete_definedfile')"
%
% EXAMPLE:
%   >> define c  2.9997e8
%   >> x = c
%   x =
%      299970000
%   >> undefine c
%   >> y = c
%   ??? Undefined function or variable 'c'.
%
%
% See also DEFINE
% Written by Ruben Faibish
% e-mail: gsx1100f@nana.co.il
% January 2007


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Getting the function parameters from the inputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



n = length(varargin);
if ~exist('definition_list.mat','file')
    disp('Nothing to undefine')
    return
end
load definition_list list
switch n
    case 0
        help undefine
        return
    case 1
        if strcmp(varargin,'-?') || strcmp(varargin,'/?')
            help undefine
            return
        elseif strcmp(varargin,'all')
            len = length(list);
            if len == 0
                disp('No constants to be undefined')
                return
            end
            newlist = list;
            for k = 1:len
                newlist =delete_definedfile(list{k},newlist);
            end
            list = newlist;
        else
            list =delete_definedfile(varargin{1},list);
        end



    otherwise
        newlist = list;
        for k = 1:n
            newlist = delete_definedfile(varargin{k},newlist);
        end
        list = newlist;
end

save definition_list list



function newlist = delete_definedfile(filename,list)

% This function deletes the constant filename from the list, deletes the
% "filename.m" file and returns the updated list as newlist

newlist = list;
n = find(strcmp(filename,list));
if ~isempty(n)
    fid = fopen([filename '.m']);
    fgetl(fid);
    str = fgetl(fid);
    if strcmp(str,'% Function Automatically Generated by Define.m, DO NOT EDIT')
        fclose(fid);
        delete([filename '.m'])
    else
        fclose(fid);
    end
    newlist(n) = [];
else
    warning('undefine:delete_definedfile',...
        ['constant ' filename ' is not defined therefore it cannot be undefined']);
end

Contact us at files@mathworks.com