Main Content

mustBeFolder

Validate that input path refers to folder

Since R2020b

    Description

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

    mustBeFolder calls the following function to determine if the input refers to a folder

    Class support: All MATLAB® classes

    example

    Examples

    collapse all

    Use the mustBeFolder function to ensure that a folder path passed to a function is a valid folder and avoid using conditional statements to test for a valid folder in the function body.

    The runInFolder function ensures that the specified folder is valid before changing to that folder to run a function.

    function r = runInFolder(path)
        arguments
            path {mustBeFolder}
        end
        orgFolder = cd(path);
        r = myFunct;
        cd(orgFolder)
    end

    Passing an invalid folder name as the input results in an error.

    r = runInFolder("myFunct.m");
    Error using runInFolder (line 3)
     r = runInFolder("myFunct.m");
                     ^^^^^^^^^^^
    Invalid argument at position 1. These folders do not exist: 'myFunct.m'.

    Input Arguments

    collapse all

    Path to a folder or folders, specified as a string scalar, character vector, string array, or cell array of character vectors.

    Example: "H:\myfolder\mysubfolder"

    Data Types: char | string

    Tips

    • mustBeFolder 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