title: Safely delete file in Matlab, without risking deletion of other files or execution of arbitrary code
body: <p>How do I safely and programmatically delete a file in Matlab? By 'safely', I mean:</p>
  
  <ol>
  <li>A single deletion statement deletes at most one file, e.g. no wildcards. <code>delete(a*)</code> should remove the file <code>a*</code> and nothing else!</li>
  <li>A single deletion statement shall not have any possible side-effects, e.g. no <code>system(sprintf('rm -f %s', foo))</code> where <code>foo</code> may be <code>file;some_command</code>.</li>
  </ol>
  
  <p>Currently, the code I encountered has <code>system(sprintf('rm -f %s', foo))</code> where there is some sanitation of <code>foo</code>. I wanted to replace it by <code>delete(foo)</code>, but according to <a href="http://www.mathworks.se/help/techdoc/ref/delete.html" rel="nofollow">documentation</a>, Matlabs <code>delete</code>-function accepts wildcards, so requirement (1) is not met; if the input contains <code>*</code>, I might end up deleting many files. How can I safely delete a file in Matlab?</p>
  
  <p>Of course, one way would be to sanitise <code>foo</code> by removing characters such as <code>:=, ()*&lt;&gt;!?%;</code>. But firstly this is a roundabout way, and secondly that doesn't allow me to remove files that contain those characters.</p>