Main Content


Read contents of file as text



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


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.

function out=fileread(filename)

Input Arguments

collapse all

Name of the file to read, specified as a character vector or string scalar. fileread leverages automatic character set detection to determine the file encoding.

Example: 'myFile.dat'

Data Types: char | string

Extended Capabilities

Introduced before R2006a