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 'fileread'.
matches = regexp(filetext,expr,'match');Display the first matching line.
disp(matches{1})function out=fileread(filename)
filename — Name of file to readName 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
'~/' or '~,
the username/'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 Example:
| ||||||||
Other folders | 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
Example:
Example:
| ||||||||
Remote Location | 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. Example:
|
Example: 'myFile.dat'
Data Types: char | string
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.
You have a modified version of this example. Do you want to open this example with your edits?