Main Content

mustBeFile

Validate that path refers to file

    Description

    example

    mustBeFile(path) throws an error if path does not refer to a file. This function does not return a value.

    mustBeFile calls the following function to determine if the input is file:

    Examples

    collapse all

    Use the mustBeFile function to ensure that a file path passed to a function is valid.

    The readLine function ensures that the file path is valid before accessing the file.

    function r = readLine(path)
        arguments
            path {mustBeFile}
        end
        fid = fopen(path);
        r = fgetl(fid);
        fclose(fid)
    end

    Passing this path to a folder to the readLine function results in an error.

    r = readLine("C:\Users\Public")
    Error using readLine
    Invalid argument at position 1. The following files do not exist:
    'C:\Users\Public'.

    Input Arguments

    collapse all

    Path to a file, specified as the fully qualified file name, including folders containing the file from the context in which the function executes.

    Example: "H:\myfolder\myfile.txt"

    Data Types: char | string

    Tips

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

    Introduced in R2020b