Read contents of file as text
Read a file and search it for text of interest.
First, read the file
fileread.m into a character vector.
filetext = fileread('fileread.m');
Then, define the text to search for.
expr = '[^\n]*fileread[^\n]*';
Find and return all lines that contain the text
matches = regexp(filetext,expr,'match');
Display the first matching line.
filename— Name of file to read
Name of file to read, specified as a character vector or string scalar that includes
the file extension.
fileread leverages automatic character set
detection to determine the file encoding.
On UNIX® systems, if
filename begins with
fileread function expands the path to the current or specified
user's home directory, respectively.
Depending on the location of your file,
filename can take on one
of these forms.
|Current folder or folder on the MATLAB® path|
Specify the name of the file in
If you open a file with read access
and the file is not in the current folder, then
If the file is not in the current folder or in a folder on the
MATLAB path, then specify the full or relative path name in
If the file is stored at a remote location, then
Based on your remote location,
If you are using a cloud file system, set environment variables to communicate with the remote file system. For more information, see Work with Remote Data.
Usage notes and limitations:
If the function
fileread reads the entire file, then all the
data must fit in the largest array that is available for code generation.