title: How to get the name of the parent folder of a file specified by its full path?
body: <p>In Matlab, I have a string containing a path to a file:</p>
  
  <pre><code>path = 'C:/Data/Matlab/Dir/file.m'
  </code></pre>
  
  <p>I want now want to extract the '<code>Dir</code>' part of the string. One way to do this is:</p>
  
  <pre><code>[first, second, third, fourth, fifth] = strtok(path, '/')
  </code></pre>
  
  <p>And then take the <code>fourth</code> element, and finally remove the first character from it (the <code>/</code>).</p>
  
  <p>I'm just wondering if there is a more elegant solution? It seems a little cumbersome to have to explicitly store all the <code>first</code> ... <code>fifth</code> elements and then manually remove the <code>/</code>.</p>
  
  <p>Thanks.</p>