Read contents of file as text
Search File for 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.
function out = fileread(filename,args)
filename — Name of file to read
character vector or string scalar
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.
encoding — Character encoding scheme
"windows-1252" | ...
Character encoding scheme associated with the file, specified as
"" or a standard character encoding scheme name like one of the
values in this table. When you do not specify any encoding or specify encoding as
fileread function uses the default
MATLAB encoding to read the file.
Encoding="system" uses the system default
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If the function
filereadreads the entire file, then all the data must fit in the largest array that is available for code generation.
The code generator for the
filereadfunction treats the char value for source or output as a signed 8-bit integer. Use values between 0 and 127 only.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version HistoryIntroduced before R2006a
R2022b: Use function in thread-based environments
This function supports thread-based environments.