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

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

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


if nargin==1
    SaveAs=0;
end

DefExt= '.mat';

[pathname,filename]= fileparts(MP.Filename);
if strcmp(filename, 'Untitled') || SaveAs
    
    % get file name
    if strcmp(filename,'Untitled')
        filename = name(MP);
    end
    if isempty(pathname)
        pathname = mbcGetPath('mbcmodel', 'Projects');
    end
    [Newfile,Newpath] = uiputfile(fullfile(pathname, [filename, DefExt]), 'Save As');
    if ischar(Newfile)
        if ~isempty(pathname) && exist([pathname, filesep, '~',filename,'_xreg.tmp'], 'file');
            delete([pathname,filesep,'~',filename,'_xreg.tmp']);
        end
        
        [~,f,e] = fileparts([Newpath,Newfile]);
        if isempty(e)
            Newfile = [f,DefExt];
        end
        
        MP= name(MP,f);
    else
        msg= '';
        return
    end
    
    [MP,msg]= RegisterFile(MP,[Newpath,Newfile]);
    if ~isempty(msg)
        return
    end
end

if nargin>2
    MP.LastNode = LastNode;
end

fname = MP.Filename;
MP.heap = [];
tmpfile = [tempname,'.mat'];
if exist(MP.Filename,'file')
    % check file is writeable befor attempting to save
    [~,att]= fileattrib(MP.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.
        MP = saveobj(MP);
        % work out MAT file format based on size
        fmt=SaveFormat(MP);
        
        save(tmpfile,'MP','-mat',fmt{:});
        
        if ~isProjectFile(MP,tmpfile)
            % save as 7.3 file
            save(tmpfile,'MP','-mat','-v7.3');
            if ~isProjectFile(MP,tmpfile)
                error('Corrupt file during save: file %s is not overwritten.',MP.filename);
            end
            
        end
        % copy to correct location
        copyfile(tmpfile,MP.Filename,'f');
        MP.Modified= 0;
        pointer(MP);
        
        msg= '';
    catch ME
        msg = ME.message;
    end
    MP.Filename = fname;
    MP.heap = [];
    pointer(MP);
    
    if exist(tmpfile,'file')
        st = recycle('off');
        delete(tmpfile);
        recycle(st);
    end
else
    msg = sprintf('%s is read-only.', MP.Filename);    
end


function fmt=SaveFormat(MP)  %#ok<INUSD>

ws=whos('MP');
% work out MAT file format based on size
if ws.bytes>1000e6
    % this format doesn't compress very well.
    fmt = {'-v7.3'};
else
    % use default format
    fmt = {};
end