www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgproject/load.m

    function [PROJ,msg]= load(PROJ,filename,allowrename,promptrename,doRegister)
%LOAD Load a project file
%
%  [P,MSG]=load(P,FILE)
%  [P,MSG]=load(P,FILE,ALLOWRENAME)
%  [P,MSG]=load(P,FILE,ALLOWRENAME,PROMPTRENAME)
%
%  If the (optional) ALLOWRENAME flag is supplied and is non-zero, the 
%  load procedure will rename and load a file if it is already open.
%  If the (optional) PROMPTRENAME flag is supplied and is non-zero, the user will
%  be prompted before a file is renamed, and given the option to cancel.
%
%  P is the loaded project.
%  MSG is an error message if there was a problem.  If there was
%  a problem, but the user has already been informed, MSG will be empty
%  but so will P.

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


msg= '';
% find what sort of variable this file contains
if exist(filename, 'file')~=2
    msg= 'File not found';
    return
end
if nargin<5
   doRegister = true;
end

try
    varlist= whos('-file',filename);
    if length(varlist)==1
        CAN_LOAD = ~strcmp(varlist.class,'struct');
    else
        msg = 'Unable to load files that were created before version 1.0';
        return
    end
catch
    % unable to read - not a mat-file?
    msg= 'Invalid file format';
    return;
end

if CAN_LOAD
    s= warning('off','all');
    lastwarn('');
    % load file
    S= struct2cell(load(filename , '-mat'));
    warning(s);
    PROJ= S{1};
    if ~isa(PROJ,'cgproject') || ~isempty(PROJ.heap)
        [msg,id]=lastwarn;
        if ~isempty(msg) && strcmp(id,'MATLAB:loadobj')
            
            pos = strfind(msg,'</a>');
            if ~isempty(pos)
                msg = msg(pos+4:end);
            end

         else
             msg= 'Invalid or corrupt file';
        end
   end
else
    msg= 'Invalid file format';
    return
end

if isempty(msg)
    PROJ.filename = filename;
    if doRegister
        if isfileopen(PROJ,filename)
            if nargin>=3 && allowrename
                if nargin>=4 && promptrename
                    answer=questdlg(['This file is already in use.  Do you '...
                        'want to open it anyway? '],...
                        'File Already Open','Yes','No','Yes');
                    drawnow;
                    if ~strcmp(answer,'Yes')
                        deleteproject(PROJ);
                        PROJ = [];
                        return;
                    end
                end
                
                
            else
                msg='File already open';
                deleteproject(PROJ);
                return
            end
        end
        % register file
        [PROJ,msg]= registerfile(PROJ,filename);
    end
end