Main Content


Validate that path refers to file

Since R2020b



    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:


    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)
            path {mustBeFile}
        fid = fopen(path);
        r = fgetl(fid);

    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:

    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


    • 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