fileread
Read contents of file as text
Description
Examples
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 'fileread'
.
matches = regexp(filetext,expr,'match');
Display the first matching line.
disp(matches{1})
function out = fileread(filename,args)
Input Arguments
filename
— Name of file to read
string scalar | character vector
Name of the file to read, specified as a string scalar or character vector 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 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 pathname in
Example:
Example:
| ||||||||
Internet URL (since R2024b) | If you specify the file as an internet uniform resource locator
(URL), then 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"
encoding
— Character encoding scheme
""
| "UTF-8"
| "ISO-8859-1"
| "windows-1251"
| "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
""
, the fileread
function uses the default
MATLAB encoding to read the file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
Example: Encoding="system"
uses the system default
encoding.
Limitations
MATLAB does not support internet URLs that require authentication.
MATLAB Online™ supports internet URLs associated with Microsoft® OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
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.The code generator for the
fileread
function treats the char value for source or output as a signed 8-bit integer. Use values between 0 and 127 only.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006aR2024b: Read data over HTTP and HTTPS using low-level file functions
You can read data from primary online sources by performing low-level file read operations over an internet URL.
R2022b: Use function in thread-based environments
This function supports thread-based environments.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)