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

    function PROJ= loadobj(PROJ)
%LOADOBJ Upgrade old versions of object
%
%  OBJ = LOADOBJ(OBJ)

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


if PROJ.version<2
    PROJ.beingdel=0;
end

if PROJ.version<3
    PROJ.modified=0;
end

if PROJ.version<4
    PROJ.SavedMBCVersion = 'Pre-2.0';
    PROJ.SavedAddonVersions = cell(0,2);
end

if PROJ.version<5
    % Add a loadstart object at the beginning of the structure to help make
    % future loads safer
    cPROJ = [{mbcloadstart}; struct2cell(PROJ)];
    sPROJ = [{'loader'}; fieldnames(PROJ)];
    PROJ = cell2struct(cPROJ, sPROJ, 1);
    % The temporary variables could be large because of the heap.  Keeping
    % them might cause two copies later on in the loadobj process
    clear('sPROJ', 'cPROJ');
end

if PROJ.version<6
    % Add a connections field.  Although this is always cleared anyway, it
    % must be added before any changes in later version upgrades.
    PROJ.Connections = [];
end

% Always clear the connections data during loading
PROJ.Connections = [];

if PROJ.version<7
    PROJ.LastNode =  [];
end
PROJ.version = 7;

% Convert to an object
if isstruct(PROJ)
   PROJ = cgproject(PROJ);
end

% Load the project into the heap and remap the pointers
if ~isempty(PROJ.heap)
    % there is some dynamic heap attached to this object
    heap= PROJ.heap;
    PROJ.heap= [];
    
    heap.info = [{PROJ}, heap.info];
    heap.ptrs = [address(PROJ)   heap.ptrs];
    
    try
        % copy old heap onto new location
        [NewPtrs,NewMap]= copy(heap.ptrs,heap.info);
    catch ME
        mbcloadrecorder('clear');
        rethrow(ME)
    end
    
    PROJ = info(NewPtrs(1));
    % play back any captured commands in recorder
    try
        EventData  = struct('LoadedPointers', {NewPtrs}, ...
            'PointerMap', {NewMap});
        recobj = mbcloadrecorder('current');
        recobj.mapptr(NewMap);
        recobj.playback(':', address(PROJ), EventData);
        % post-load operations might update the project object on the heap
        PROJ = info(PROJ);
        % Clear recorder ready for next load
        mbcloadrecorder('clear');
    catch ME
        try
            % delete new tree and continue
            deleteproject(PROJ);
        end
        mbcloadrecorder('clear');
        rethrow(ME)
    end
    
    
    try
        % Check nodes all seem OK
        preorder(PROJ, @checknode);
    catch ME
        try
            % delete new tree and continue
            deleteproject(PROJ);
        end
        if strcmp(ME.identifier,'MATLAB:UndefinedFunction') && ~isempty(strfind(ME.message,'''checknode'''))
            % an internal CAGE object has not loaded correctly. Make a more
            % user friendly error message
            error(message('mbc:cgproject:InvalidObject'));
        else
            rethrow(ME)
        end
    end
end