This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Read contents of file as text


text = fileread(filename)



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 Contents.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.

Example: 'myFile.dat'

Data Types: char | string

Introduced before R2006a