confirmation = confirm_overwrite_file(filepath) confirmation is 1 or 0, indicating whether or not filepath exists after this function is run. (in other words, if the file was deleted by this function or the file checks to see if a file designated by filepath exists. If it does exist, prompts the user whether he/she want's to overwrite the file. If the user answers 'y' the file is deleted. This utility is useful when a function makes repeated 'print' or fprintf calls, appending to a plot or log file and it is desireable to delete the plot or log file at the begninning of the process. User confirmation ensures that useful plots or logs are not simply overwritten, 12 July 2007, Stefan Tomic
0001 function confirmation = confirm_overwrite_file(filepath) 0002 % 0003 % confirmation = confirm_overwrite_file(filepath) 0004 % 0005 % confirmation is 1 or 0, indicating whether or not filepath exists after 0006 % this function is run. (in other words, if the file was deleted by 0007 % this function or the file 0008 % 0009 % checks to see if a file designated by filepath exists. If it does 0010 % exist, prompts the user whether he/she want's to overwrite the 0011 % file. If the user answers 'y' the file is deleted. This utility 0012 % is useful when a function makes repeated 'print' or fprintf 0013 % calls, appending to a plot or log file and it is desireable to 0014 % delete the plot or log file at the begninning of the 0015 % process. User confirmation ensures that useful plots or logs are 0016 % not simply overwritten, 0017 % 0018 % 12 July 2007, Stefan Tomic 0019 0020 if(exist(filepath,'file')) 0021 0022 confirmOverwrite = ''; 0023 while(~ismember(confirmOverwrite,{'y','n'})) 0024 confirmOverwrite = input(sprintf('File %s exists, overwrite? (y/n): ',filepath),'s'); 0025 end 0026 0027 if(strcmp(confirmOverwrite,'y')) 0028 delete(filepath); 0029 confirmation = 1; 0030 else 0031 confirmation = 0; 0032 end 0033 0034 else 0035 confirmation = 1; 0036 end 0037 0038 0039 0040 0041 0042 0043 return