Validate path syntax, type, and extension

Check paths before saving/exporting, with human readable feedback.
Updated 19 Dec 2023

ispathvalid & mustBeValidPath: Validate path syntax, type, and extension

View on GitHub View on File Exchange

The typical use case is to check paths at the start of a script/function before saving/exporting, with human readable feedback if validation fails.


tf = isvalidpath( inputPath ) checks if the platform can parse the path inputPath. Unlike isfolder and isfile, it does not verify that the path exists in the file system.

tf = isvalidpath( inputPath, pathType ) also checks that the location of inputPath is either a file or directory.

tf = isvalidpath( inputPath, "file", validExtensions ) also checks that inputPath contains a file extension from a provided set, validExtensions.

[ tf, Log ] = isvalidpath( __ ) additionally returns formatted log messages. Log.warning explains why a path is not valid. provides formatting tips. Use the disp function to print the log to the command window.

mustBeValidPath( inputPath, pathType, validExtensions ) function identically to isvalidpath but throws an error if the path format is not valid. pathType and validExtensions are optional.


Argument Description
inputPath Path to validate.
  • Supported path formats:
    • Traditional path, to a file or directory, relative or absolute, e.g., C:\ffmpeg and ../file.dat.
  • Unsupported path formats:
    • Remote locations, e.g., s3://bucketname/.
    • UNC paths, e.g., \\\temp.
    • DOS device paths, e.g., \\.\UNC\Server\Share\.
    • URIs, e.g., file://C:/file.dat and
    • All others not listed here.
Text scalar, i.e., character vector or string scalar.
pathType Valid location type of inputPath. Either:
  • "any" - (default) Any path type is acceptable.
  • "file" - Only a file path is acceptable, i.e., it must have a file name and a valid extension according to validExtensions.
  • "dir" - Only a directory path is acceptable, i.e., it cannot have a file name or extension.
  • "directory" - Same as "dir". Text scalar, i.e., character vector or string scalar.
validExtensions Specifies which file extensions are valid if the input is a file path. Each entry must be either:
  • (default) A period (.), representing any extension.
  • Text beginning with a period character, e.g., ".mat".
  • Empty text, {''} or "", representing no extension.
  • "image", representing all raster image extensions MATLAB knows. Run the command [ imformats().ext ] to see the list.
Character vector, cell array of character vectors, or string array.


Argument Description
tf Whether the path valid or not according to the above options.
Logical scalar.
Log Formatted log messages. Struct scalar with the fields:
  • warning - String. Explains why the path is not valid, or states when the location is ambiguous, e.g., "C:\example" could be either a directory or a file without an extension. Possible warning messages:
    • Invalid path as per platform rules, e.g., "dir\dir\dir\:".
    • Directory path includes a file extension, e.g., "dir\file.ext".
    • File path lacks a file name, e.g., "dir\".
    • File path missing valid user-specified extension, e.g., "dir\file".
    • Path could be a file OR a directory, e.g., "dir\ambiguous". The path may still be valid.
  • info - String. Contains additional formatting information and explains formatting issues which will not affect the use of path in practice, as they are handled correctly by the platform. Possible info messages:
    • Path altered by platform during parsing, e.g., any of the below.
    • Redundant name elements removed by platform, e.g., ".\dir".
    • Path has incorrect separators for platform, e.g., "dir\dir/dir".
    • Path includes consecutive separators, e.g., "dir//dir/".
If there are no messages, the fields of Log will be "", i.e., a zero length string. Use the disp function to print the messages to the command window, e.g., disp( Log.warning ).


load( "spine.mat", "X" )
X = X / max( X, [], "all" );
outputFile = "output\xray.jpg";
validExts = ["" ".mat" "image"];
[ isSave, Log ] = isvalidpath( outputFile, "file", validExts );
if isSave
    [ filePath, ~, fileExt ] = fileparts( outputFile );
    if ~isfolder( filePath )
        [ status, msg ] = mkdir( filePath );
        assert( status == 1, ...
            "Could not make output directory:\n -\t%s", msg )
        if strcmp( fileExt, "" ) || strcmp( fileExt, ".mat" )
            save( outputFile, "X" )
            imwrite( X, outputFile )
        fprintf( "Saved patient scan to '%s'.\n", outputFile )
    catch ME
        warning( ME.identifier, ...
            "Patient scan not saved:\n -\t%s", ME.message )
    warning( "Patient scan not saved. " + ...
        "outputFile is not a valid.\n\n%s\n", Log.warning )


Created in 2022b. Compatible with MATLAB release 2019b and later. Compatible with all platforms.

Published under MIT License (see LICENSE.txt).

Please cite George Abrahams (GitHub, LinkedIn, Google Scholar).

Cite As

George Abrahams ( 2023. Validate path syntax, type, and extension (, GitHub.

MATLAB Release Compatibility
Created with R2022b
Compatible with R2019b and later releases
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.