Home > . > fileext.m

fileext

PURPOSE ^

FILEEXT - utility to parse a file name into name and extention components

SYNOPSIS ^

function [file, ext, dirPath] = fileext(inputFileName)

DESCRIPTION ^

 FILEEXT - utility to parse a file name into name and extention components

 [file, ext, dirPath] = fileext(inputFileName);

 inputFileName     1x1 or 1x2 cell containing a string with name of input 
                   file (must include file extention) and an optional
                   field path for *.ilwd files; if 1x2 cell, second
                   component is ignored

 file              file name without extention (string)
 ext               file extension (string)
 dirPath           (optional) file directory path (string)

 SEE ALSO: GRAVEN

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [file, ext, dirPath] = fileext(inputFileName)
0002 
0003 % FILEEXT - utility to parse a file name into name and extention components
0004 %
0005 % [file, ext, dirPath] = fileext(inputFileName);
0006 %
0007 % inputFileName     1x1 or 1x2 cell containing a string with name of input
0008 %                   file (must include file extention) and an optional
0009 %                   field path for *.ilwd files; if 1x2 cell, second
0010 %                   component is ignored
0011 %
0012 % file              file name without extention (string)
0013 % ext               file extension (string)
0014 % dirPath           (optional) file directory path (string)
0015 %
0016 % SEE ALSO: GRAVEN
0017 
0018 % $Id: fileext.m,v 1.7 2004/05/18 21:10:10 stuver Exp $
0019 
0020 % INITIALIZE output variables
0021 % IF inputFileName is a cell
0022 %   ASSIGN fileName
0023 %   CHECK for errors
0024 % ELSEIF inputFileName is a string
0025 %   ASSIGN fileName
0026 % ELSE
0027 %   RETURN error
0028 % END
0029 % FIND indicies that are '.'
0030 % ASSIGN file
0031 % ASSIGN ext
0032 % RETURN
0033 
0034 [file, ext, dirPath] = deal([]);
0035 
0036 if strcmp(class(inputFileName), 'cell')
0037     fileName = inputFileName{1};
0038     if ~strcmp(class(fileName), 'char')
0039         fileName
0040         error('fileext: fileName must be a string')
0041     end
0042 elseif strcmp(class(inputFileName), 'char')
0043     fileName = inputFileName;
0044 else
0045     inputFileName
0046     error('fileext: inputFileName must be either cell or char class')
0047 end
0048 
0049 I = find(fileName == '.');
0050 % do error checking to make sure that I ~= end & there is not more
0051 % than 1 . Check to make sure I is not empty.
0052 
0053 if isempty(I)
0054     inputFileName
0055     error(['fileext: inputFileName starts with a . and does not have a '...
0056             'valid directory path'])
0057 end
0058 
0059 if length(I) > 1 & I(1) == 1
0060     % There is a directory path in the file name
0061     D = find(fileName == '/' | fileName == '\');
0062     % error check that D is not empty
0063     if isempty(D)
0064         inputFileName
0065         error(['fileext: inputFileName starts with a . and does not have '...
0066                 'a valid directory path'])
0067     end
0068     file = fileName(D(end)+1:I(end)-1);
0069     ext = fileName(I(end)+1:end);
0070     dirPath = fileName(1:D(end));
0071 elseif length(I) == 1 & I(1) ~=1 & I(1) ~= length(fileName)
0072     % There is no directory path in filename
0073     file = fileName(1:I(end)-1);
0074     ext = fileName(I(end)+1:end);
0075 else
0076     inputFileName
0077     error('fileext: inputFileName is improperly formatted')
0078 end
0079 
0080 % Insure that the strings come out as row arrays
0081 file = file(:)';
0082 ext = ext(:)';
0083 dirPath = dirPath(:)';
0084 
0085 return

Generated on Tue 05-Oct-2004 10:40:50 by m2html © 2003