fileread - Read contents of file as text - MATLAB (original) (raw)
Read contents of file as text
Syntax
Description
text = fileread([filename](#mw%5F22fb7fdc-1268-4e2a-be26-f4b3257500be))
returns contents of the file filename
as a character vector.
text = fileread([filename](#mw%5F22fb7fdc-1268-4e2a-be26-f4b3257500be),Encoding=[encoding](#mw%5F363208eb-fec7-45b2-abc4-74214961965c))
opens filename using the encoding specified by encoding
.
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.
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 '~_`username`_/'
, the 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 infilename.If you open a file with read access and the file is not in the current folder, then fileread searches along the MATLAB search path.Example: "sample_file.txt" |
---|---|
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 infilename.Example: "C:\myFolder\myFile.sample_file.txt"Example: "myFolder\sample_file.txt" |
Internet URL (since R2024b) | If you specify the file as an internet uniform resource locator (URL), then filename must include the protocol type"http://" or"https://".Example: "http://hostname/path\_to\_file/my\_data.csv" |
Remote location | If the file is stored at a remote location, thenfilename must contain the full path of the file specified as a URL of the form:scheme_name://path_to_file/_my_file.ext_Based on your remote location,scheme_name can be one of the values in this table. Remote Locationscheme_nameAmazon S3™s3Windows Azure® Blob Storagewasb, wasbsHDFS™hdfsIf 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: "s3://bucketname/path_to_file/sample_file.txt" |
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.
"Big5" | "ISO-8859-1" | "windows-874" |
---|---|---|
"Big5-HKSCS" | "ISO-8859-2" | "windows-949" |
"CP949" | "ISO-8859-3" | "windows-1250" |
"EUC-KR" | "ISO-8859-4" | "windows-1251" |
"EUC-JP" | "ISO-8859-5" | "windows-1252" |
"EUC-TW" | "ISO-8859-6" | "windows-1253" |
"GB18030" | "ISO-8859-7" | "windows-1254" |
"GB2312" | "ISO-8859-8" | "windows-1255" |
"GBK" | "ISO-8859-9" | "windows-1256" |
"IBM866" | "ISO-8859-11" | "windows-1257" |
"KOI8-R" | "ISO-8859-13" | "windows-1258" |
"KOI8-U" | "ISO-8859-15" | "US-ASCII" |
"Macintosh" | "UTF-8" | |
"Shift_JIS" |
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.
Version History
Introduced before R2006a
You can read data from primary online sources by performing low-level file read operations over an internet URL.
This function supports thread-based environments.