Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
Created March 31, 2012 22:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ferryzhou/2269380 to your computer and use it in GitHub Desktop.
Save ferryzhou/2269380 to your computer and use it in GitHub Desktop.
matlab mkdir if not exist
function mkdir_if_not_exist(dirpath)
if dirpath(end) ~= '/', dirpath = [dirpath '/']; end
if (exist(dirpath, 'dir') == 0), mkdir(dirpath); end
end
@sirius27
Copy link

Hi ferry you can simplify the code a little bit:

if ~exist(dirpath,'dir') mkdir(dirpath); end

@mianabdullah
Copy link

Thank you Zhou and Sirius27

@waldyrious
Copy link

For creating a single directory at the level of the script where this code is called, I found it cleaner to use a one-liner, rather than define a function:

if (~exist('<folder_name>', 'dir')); mkdir('<folder_name>'); end%if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment