Contenido principal

mustBeFilePathInclusive

R2026b

Validate that input is file in current folder, specified location, or MATLAB path

Since R2026b

    Description

    mustBeFilePathInclusive(value) throws an error if value is not a file in the current folder, at the specified location, or on the MATLAB® path. This function does not return a value.

    mustBeFilePathInclusive calls the isfilePathInclusive function to determine if the input is the name of a file that exists in an inclusive set of locations.

    example

    Examples

    collapse all

    Use the mustBeFilePathInclusive function to ensure that a file path passed to a function refers to a file in the current folder, at the specified location, or on the MATLAB path.

    The loadData function ensures that the input is the name of a file that exists in the current folder, at the specified location, or on the MATLAB path before reading it.

    function T = loadData(filename)
        arguments
            filename {mustBeFilePathInclusive}
        end
        T = readtable(filename);
    end

    Passing the name of a file that does not exist in any of the expected locations results in an error.

    T = loadData("nonexistentFile.csv")
    Error using loadData (line 3)
     T = loadData("nonexistentFile.csv")
                  ^^^^^^^^^^^^^^^^^^^^^
    Invalid argument at position 1. These files do not exist: 'nonexistentFile.csv'.

    Use mustBeFilePathInclusive as a property validator to ensure that a property value refers to a valid file.

    This class restricts the value of DataFile to a file in the current folder, at the specified location, or on the MATLAB path.

    classdef DataReader
       properties
          DataFile {mustBeFilePathInclusive}
       end
    end

    Create an object and assign a value to its property.

    obj = DataReader;
    obj.DataFile = "invalidPath/missing.txt";
    Error using implicit default value of property 'DataFile' of class 'DataReader'.
    Value must be text with one or more characters.

    Input Arguments

    collapse all

    Value to validate, specified as a scalar or array of any MATLAB type or class.

    Example: "myFile.txt"

    Example: "C:\Work\mydata.csv"

    Tips

    • mustBeFilePathInclusive is designed to be used for property and function argument validation.

    Version History

    Introduced in R2026b