Main Content

mustBeFile

Validate that path refers to file

Since R2020b

    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
     r = readLine("C:\Users\Public")
                  ↑
    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.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2020b