Matlab: check if string has a correct file path syntax

Viewed 1132

I want to check if a string represents a full path of a file, like this:

p = 'C:\my\custom\path.txt'

The file does not exist, so commands like isdir and exist return false to me, but still the format of the string represents a valid path for my operating system, while the following one does not because it has an invalid character for the file name:

p = 'C:\my\custom\:path.txt'

So I'd like to know how to check if a string represents a valid file path without needing that the file actually exists.

3 Answers
function bool = isLegalPath(str)
    bool = true;
    try
        java.io.File(str).toPath;
    catch
        bool = false;
    end
end
Related