# v_fopenmkd

## PURPOSE

V_FOPENMKD is the same as FOPEN but creates any missing directories [fid,mes]=(fn,pe,mf,en)

## SYNOPSIS

function [fid,mes]=v_fopenmkd(fn,pe,mf,en)

## DESCRIPTION

    V_FOPENMKD is the same as FOPEN but creates any missing directories [fid,mes]=(fn,pe,mf,en)

     This procedure is functionally identical to fopen() except that
     it will create the requested folder if it doesn't exist

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [fid,mes]=v_fopenmkd(fn,pe,mf,en)
    0002 %V_FOPENMKD is the same as FOPEN but creates any missing directories [fid,mes]=(fn,pe,mf,en)
    0003 %
    0004 % This procedure is functionally identical to fopen() except that
    0005 % it will create the requested folder if it doesn't exist
    0006
    0007 %       Copyright (C) Mike Brookes 2011
    0008 %      Version: $Id: v_fopenmkd.m 10865 2018-09-21 17:22:45Z dmb $
    0009 %
    0010 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0011 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0012 %
    0013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0014 %   This program is free software; you can redistribute it and/or modify
    0015 %   it under the terms of the GNU Lesser General Public License as published by
    0016 %   the Free Software Foundation; either version 3 of the License, or
    0017 %   (at your option) any later version.
    0018 %
    0019 %   This program is distributed in the hope that it will be useful,
    0020 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0021 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0022 %   GNU Lesser General Public License for more details.
    0023 %
    0024 %   You can obtain a copy of the GNU Lesser General Public License from
    0025 %   https://www.gnu.org/licenses/ .
    0026 %
    0027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0028 p=fileparts(fn);
    0029 switch nargin  % first try calling fopen normally
    0030     case 1
    0031         [fid,mes]=fopen(fn);
    0032     case 2
    0033         [fid,mes]=fopen(fn,pe);
    0034     case 3
    0035         [fid,mes]=fopen(fn,pe,mf);
    0036     otherwise
    0037         [fid,mes]=fopen(fn,pe,mf,en);
    0038 end
    0039 if fid<0  % if it was unsuccessful, check if the directory exists
    0040     ff=dir(p);
    0041     if ~numel(ff)
    0042         st=mkdir(p); % if not, create the directory
    0043         if ~st
    0044             error('Cannot create directory/folder: %s',p);
    0045         end
    0046     elseif ~ff(1).isdir
    0047         error('Directory/folder name %s is an existing file.',p);
    0048     end
    0049     switch nargin
    0050         case 1
    0051             [fid,mes]=fopen(fn);
    0052         case 2
    0053             [fid,mes]=fopen(fn,pe);
    0054         case 3
    0055             [fid,mes]=fopen(fn,pe,mf);
    0056         otherwise
    0057             [fid,mes]=fopen(fn,pe,mf,en);
    0058     end
    0059 end