Main Content

fileread

Read contents of file as text

Description

text = fileread(filename) returns contents of the file filename as a character vector.

example

text = fileread(filename,Encoding=encoding) opens filename using the encoding specified by encoding.

example

Examples

collapse all

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

collapse all

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 in filename.

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 in filename.

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, then filename 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_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

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: "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

Version History

Introduced before R2006a

expand all