fileread
Read contents of file as text
Description
Examples
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
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"
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
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.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006aYou can read data from primary online sources by performing low-level file read operations over an internet URL.
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)