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

define(arg1,arg2,arg3)
function define(arg1,arg2,arg3)

% DEFINE Creates Constants without the use of global
%   DEFINE('CONSTNAME','CONSTVAL') creates a constant named CONSTNAME whose
%   value is CONSTVAL
%   
%   USAGE:
%            DEFINE
%            DEFINE /? or DEFINE -?
%            DEFINE list
%            DEFINE('CONSTNAME','CONSTVAL');
%            DEFINE('CLASSTYPE','CONSTNAME','CONSTVAL');
%   
%   DEFINE with no argument or DEFINE -? or DEFINE /? gives this help on
%   the DEFINE function
%   
%   DEFINE('CONSTNAME','CONSTVAL') or DEFINE CONSTNAME CONSTVAL defines a
%   constant named CONSTNAME whose value is CONSTVALUE. CONSTNAME must be a
%   valid MATLAB name. CONSTVAL must be a valid matlab expression
%   
%   DEFINE('CLASSTYPE','CONSTNAME','CONSTVAL') or
%   DEFINE CLASSTYPE CONSTNAME CONSTVAL is the same as with 2 arguments
%   with CONSTNAME constrained to be of CLASSTYPE class. the class type can
%   be any MATLAB class or user defined class.
%   WARNING: No verification is made to check that the casting from
%   CONSTVAL to the CLASSTYPE class is made.
%   
%   The DEFINE function creates a MATLAB function with the same name as the
%   CONSTNAME argument and which returns the same value as the CONSTVAL
%   argument. This file is located in the same folder from which the DEFINE
%   function is called.
%   The function stores the list of all the defined constants in a list
%   cell saved in a file named "definition_list.mat". This file is
%   automatically updated each time the function is used. 
%   The created files has as H1 line the following string:
%        "% Function Automatically Generated by Define.m, DO NOT EDIT".
%   This line is used by the DEFINE and the UNDEFINE function in order to 
%   identify that the file was created by the DEFINE functionand prevent 
%   overwritting an existing file with the same name which was not created
%   by the DEFINE function. Do not edit the M-file.
%   Both the M-files and the definition_list file are created in the 
%   current folder. Different projects located at different folders may
%   have constants defined by the DEFINE function with the same name and
%   different values.
% RESTRICTIONS:
%   On Windows platform, file names are case insensitive, there cannot be
%   in the same folder two files with the same name but differenr cases
%   (such as c.m and C.m). 
% 
% EXAMPLE:
%   (1)
%   >> define c 2.9997e8;
%   >> x = 2*c
%   x =
%      599940000
% 
%   (2)
%   >> define int16 intpi pi;
%   >> y = intpi
%   y =
%         3
%   >> whos
%     Name      Size            Bytes  Class    Attributes
%     y         1x1                 2  int16              
%
% 
% See also UNDEFINE
% Written by Ruben Faibish
% e-mail: gsx1100f@nana.co.il
% January 2007


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

switch nargin
    case 0
        help define
        return
    case 1
        if strcmp(arg1,'/?')||strcmp(arg1,'-?')
            help define
            return
        elseif strcmp(arg1,'list');
            if exist([cd filesep 'definition_list.mat'],'file');
                load([cd filesep 'definition_list.mat'])
                disp('The currently defined constants are:')
                disp(list)
            else
                disp('The currently defined constants list is empty');
            end
            return
        else
            error('define:arguments','Not Enough Arguments')
        end
    case 2
        constant_type = 'double';
        filename = arg1;
        value = arg2;
    case 3
        constant_type = arg1;
        filename = arg2;
        value = arg3;
    case 4
        error('define:arguments','Too Many Arguments')
end

if ~isvarname(filename)
    error('define:arguments',[filename ' is not a valid name that can be used by MATLAB']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Creating the constant value M-file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if exist([cd filesep filename '.m'],'file');
    fid = fopen([filename '.m']);
    fgetl(fid);
    str = fgetl(fid);
    if ~strcmp(str,'% Function Automatically Generated by Define.m, DO NOT EDIT')
        fclose(fid);
        error('define:FileCreation',['A file named ''' filename '.m'' allready exists which is not a definition file'])
    end
end

% eval(['fid = fopen(''' filename ''',''w'');'])
fid = fopen([filename '.m'],'w');
fprintfstring = ['function y = '  filename];
fprintf(fid,fprintfstring);
fprintf(fid,'\n');
fprintf(fid,'%% Function Automatically Generated by Define.m, DO NOT EDIT');
fprintf(fid,'\n');
fprintfstring = ['y = ' constant_type '(' value ');'];
fprintf(fid,fprintfstring);
fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Updating the definition list
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('definition_list.mat','file')
    list{1} = filename;
    
else
    load definition_list list
    n = find(strcmp(list,filename));
    len = length(list);
    if isempty(n)
        list{len+1} = filename;
    else
        list{n} = filename;
    end
end
save definition_list list
   

Contact us at files@mathworks.com