mustBeFile

Checks that text names existing files.

Category: Programming > Functions

Syntax

mustBeFile(A)

Checks that every path in A refers to an existing file. Returns nothing if the check passes and throws an error if not.

Description

mustBeFile is a validator: it inspects its input and either returns silently or raises an error with the identifier MATLAB:validators:mustBeFile. The error message states the requirement that was violated, so the caller sees what was expected rather than a generic failure.

A can be a character vector, a string array or a cell array of character vectors. A path that names a folder does not pass, since the check is for files. Relative paths are resolved against the current folder.

Input arguments

A
The value to check. Any array of the class the validator accepts.

Example

Passing and failing input

fid = fopen('check.txt', 'w'); fclose(fid);
mustBeFile('check.txt');
disp('passed')
try
    mustBeFile('nofile.xyz')
catch err
    disp(err.message)
end
delete('check.txt')
Output
passed
The following files do not exist: 'nofile.xyz'.

See also

mustBeFolder isfile exist mustBeText