title: Using uigetfile instead of uigetdir to get directories in Matlab
body: <p>So I have a question about MATLAB directory selection gui. I need to use a GUI to select a directory, but the problem is that the uigetdir interface is awful. If I call like this:</p>
  
  <pre><code>blah = uigetfile('C:\...\T2 Measurements');
  </code></pre>
  
  <p>This is what it shows me:</p>
  
  <p><img src="https://i.stack.imgur.com/Qo9C3.jpg" alt="uigetdir results"></p>
  
  <p>As you can see, this is awful. There's a ton of extraneous information about the location of the file in the filesystem and the relevant information is all below the fold. Ideally, I'd like to specify that the uigetdir function use the uigetfile GUI, or just pass an argument to uigetfile telling it that I'm looking for a directory, not a single file, since this is what the uigetfile GUI looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/jM93M.png" alt="uigetfile results"></p>
  
  <p>But of course, this requires that I select a file, not a directory. Obviously the directories are not open, so I suppose I could just have the user select any random file in the folder and I can get the pathname, but is there a better way to do this? In another application, I could imagine that my "select a file in the folder" workaround wouldn't work.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I made some very minor adjustments to Andrew Janke's code to make it take the same arguments as uigetdir(). Here's what I came up with:</p>
  
  <pre><code>function [pathname] = uigetdir2(start_path, dialog_title)
  % Pick a directory with the Java widgets instead of uigetdir
  
  import javax.swing.JFileChooser;
  
  if nargin == 0 || start_path == '' || start_path == 0 % Allow a null argument.
      start_path = pwd;
  end
  
  jchooser = javaObjectEDT('javax.swing.JFileChooser', start_path);
  
  jchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  if nargin &gt; 1
      jchooser.setDialogTitle(dialog_title);
  end
  
  status = jchooser.showOpenDialog([]);
  
  if status == JFileChooser.APPROVE_OPTION
      jFile = jchooser.getSelectedFile();
      pathname = char(jFile.getPath());
  elseif status == JFileChooser.CANCEL_OPTION
      pathname = [];
  else
      error('Error occured while picking file.');
  end
  </code></pre>