title: Error writing image to file in Matlab
body: <p>I am trying to write an image that I do operations on to a '.tif' file in a directory. I make the results directory with Matlab using the mkdir() function.</p>
  
  <p>Here is the command I am using:</p>
  
  <pre><code>[pathstr, nameWOext, ext] = fileparts(filename);
  results_dir = ['results' '/results_' nameWOext];
  
  %check to see if the directory exists already, if it doesn't make it
  if(exist(results_dir) ~= 7)
      mkdir(results_dir);
  end
  
  filenamezero = [nameWOext '_J' ext];
  
  imwrite (~J, fullfile(results_dir, filenamezero));  //Error here
  </code></pre>
  
  <p>When Matlab gets to this line it outputs an error: </p>
  
  <blockquote>
    <p>Could not open file for writing.  Check directory or file permissions.</p>
  </blockquote>
  
  <p>I inspected the folder 'results/results_' and the folder is read-only. Apparently <code>mkdir()</code> is doing this automatically. </p>
  
  <p>Is there anyway to get around this? </p>
  
  <p>Thanks</p>
  
  <p>P.S. I am running Windows 7 using Matlab 6.1</p>