www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/mbcinstalladdon.m

    function [ok, msg] = mbcinstalladdon(pth, mode)
%MBCINSTALLADDON  Install an add-on structure
%
%  [OK, MSG] = MBCINSTALLADDON(PATH)  installs the add-on package located
%  in the folder PATH.  This includes adding it to the path and executing
%  an installation function if necessary.

%  OK = MBCINSTALLADDON(PATH, MODE) allows user to control whether the path
%  is made persistent via savepath of not. MODE can be -savepath or -nosave

%  Copyright 2000-2014 The MathWorks, Inc. and Ford Global Technologies, Inc.


ok = 1;
msg = '';

% default behaviour is to save path
if nargin==1
    mode = '-savepath';
elseif ~(strcmpi(mode, '-savepath') || strcmpi(mode, '-nosave'))
    error(message('mbc:mbcinstalladdon:InvalidArgument'));
end

def_file = fullfile(pth,'mbcextras.mbc');
if exist(def_file,'file')
    try
        S = load(def_file,'-mat');
    catch
        ok = 0;
        msg = 'The addon definition file is not a valid MATLAB file.';
        return
    end
    if isfield(S,'MBC_EXTENSION_PACKAGE')
        if S.MBC_EXTENSION_PACKAGE
            % add to path
            if length(S.Path)
                add_pth = cell(size(S.Path));
                for n = 1:length(S.Path)
                    add_pth{n} = fullfile(pth, S.Path{n});
                end
                addpath(add_pth{:});
                if strcmpi(mode, '-savepath')
                    savepath;
                end
            end
            % Execute installation function if it is defined
            if ~isempty(S.InstallFcn)
                try
                    feval(S.InstallFcn);
                catch
                    ok = 0;
                    msg = 'The custom installation function caused an error.';
                    return
                end
            end
            ok = 1;
            
            % update information about extensions
            mbcextensions.Extensions.update;
        end
    end
else
    ok = 0;
    msg = 'The specified addon definition file does not exist.';
end