To check if an input is a valid text file name in MATLAB, you can use the exist
function to determine if the file exists and is a plain text file. Here's an example function that uses this approach:
main.m405 chars22 lines
In this function, the exist
function is used to check if the file exists by returning a non-zero value (2) if it does. Then, the fopen
function is used to attempt to open the file for reading. If the file can't be opened, the function returns false. Otherwise, the fgetl
function is used to read the first line of the file. If the result is -1, then the file is empty. If the result is a string, then the file is not empty. If the result is an empty string, then the file is a binary file. By comparing the result of fgetl
to -1, the function determines if the file is a plain text file.
gistlibby LogSnag