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

    function [PROJ,msg]= save(PROJ,SaveAs,LastNode)
%SAVE Save a project
%
%  [P, MSG] = SAVE(P, SAVEASFLAG) saves the project P. If SAVEASFLAG is
%  true, the user is prompted for a filename.

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

if nargin==1
    SaveAs=0;
end

[pathname,filename] = fileparts(PROJ.filename);
if strcmp(filename,'Untitled') || SaveAs
    % get file name
    [Newfile,Newpath] = uiputfile(mbcGetPath('cage', 'Projects', '*.cag'),'Save As');
    if Newfile ~= 0
        if ~isempty(pathname) && exist([pathname,filesep,'~',filename,'_cage.tmp'], 'file');
            delete([pathname,filesep,'~',filename,'_cage.tmp']);
        end
        % add ".cag" if appropriate
        doti = strfind(Newfile,'.');
        if isempty(doti)
            Newfile = [Newfile '.cag'];
        end
    else
        msg = 'Cancel';
        return
    end

    [PROJ,msg] = registerfile(PROJ,[Newpath,Newfile]);
    if ~isempty(msg)
        return
    end
end

tmpfile = [tempname,'.mat'];
if exist(PROJ.filename,'file')
    % check file is writeable befor attempting to save
    [~,att]= fileattrib(PROJ.filename);
    CanSave = att.UserWrite;
else
    % always try saving if file doesn't exist. The file system will throw
    % an error if users cannot write to the specified location 
    CanSave = true;
end
if CanSave
    try
        % get static version for saving. Doing this task here gives protection
        % against failure in this routine.
        PROJ.heap = [];
        PROJ = saveobj(PROJ);
        fmt=SaveFormat(PROJ);
        
        if nargin>2
            PROJ.LastNode = LastNode;
        else
            PROJ.LastNode = [];
        end
        save(tmpfile,'PROJ','-mat',fmt{:});
        
        if ~isProjectFile(PROJ,tmpfile)
            % save as 7.3 file
            save(tmpfile,'PROJ','-mat','-v7.3');
            if ~isProjectFile(PROJ,tmpfile)
                error('Corrupt file during save: file %s is not overwritten');
            end
        end
        % copy to correct location
        copyfile(tmpfile,PROJ.filename,'f');
        msg = '';
    catch ME
        msg = ME.message;
    end
    if exist(tmpfile,'file')
        st = recycle('off');
        delete(tmpfile);
        recycle(st);
    end
    PROJ.heap = [];
    pointer(PROJ);
else
    msg = sprintf('%s is read-only.', PROJ.filename);
end

function fmt=SaveFormat(PROJ)  %#ok<INUSD>
% work out MAT file format based on size

ws=whos('PROJ');
if ws.bytes>1000e6
    % this format doesn't compress very well. 
    fmt = {'-v7.3'};
else
    % use default format
    fmt = {};
end