📄 mbdelete.m
字号:
function deleteSuccessful = mbdelete(fileName, showWarnings, throwError)
%MBDELETE Delete file and check possible warnings.
% RESULT = MBDELETE(FILE) will check if file FILE is existing, try to
% delete the file and check if the deletion caused a warning afterwards.
% If the file could be deleted or was not existing, TRUE is returned,
% otherwise FALSE.
%
% RESULT = MBDELETE(FILE, SHOWWARNING, THROWERROR) with THROWERROR
% evaluating to TRUE will cause MBDELETE to throw an error if Matlab
% would have given a warning. With SHOWWARNING evaluating to false,
% MBDELETE will not display a warning. Default values are THROWERROR =
% FALSE and SHOWWARNING = TRUE.
%
% Markus Buehren
% Last modified 17.01.2009
%
% See also DELETE, EXISTFILE, WARNING, LASTWARN.
warnMode = 0;
errorMode = 0;
if existfile(fileName)
% turn off file permission warnings
warnID = 'MATLAB:DELETE:Permission';
warnState = warning('query', warnID);
warning('off', warnID);
% reset warnings and errors
lastwarn('');
lasterror('reset');
% try to delete file
try
delete(fileName); %% file access %%
% check last warning
[lastMsg, lastWarnID] = lastwarn;
deleteSuccessful = ~strcmp(lastWarnID, warnID);
catch
% deleting caused an error
deleteSuccessful = false;
if errorMode
disp(sprintf('Error thrown when trying to remove file %s:', fileName));
displayerrorstruct;
end
end
% warn or throw error if deletion was not successful
if ~deleteSuccessful
if ~exist('throwError', 'var')
throwError = 0;
end
if ~exist('showWarnings', 'var')
showWarnings = 1;
end
if throwError
error('Error: Unable to remove file %s.', fileName);
elseif showWarnings
disp(sprintf('Warning: Unable to remove file %s.', fileName));
% display warning (if any)
if warnMode && ~isempty(lastMsg)
disp(sprintf('Warning issued when trying to remove file %s:\n%s', ...
fileName, lastMsg));
end
end
end
% reset warning state
warning(warnState);
else
deleteSuccessful = true;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -