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 text from PDF, Microsoft Word, HTML, and plain text files


str = extractFileText(filename)
str = extractFileText(filename,Name,Value)



str = extractFileText(filename) reads the text data from a file as a string.


str = extractFileText(filename,Name,Value) specifies additional options using one or more name-value pair arguments.


collapse all

Extract the text from sonnets.txt using extractFileText. The file sonnets.txt contains Shakespeare's sonnets in plain text.

str = extractFileText("sonnets.txt");

View the first sonnet.

i = strfind(str,"I");
ii = strfind(str,"II");
start = i(1);
fin = ii(1);
ans = 
       From fairest creatures we desire increase,
       That thereby beauty's rose might never die,
       But as the riper should by time decease,
       His tender heir might bear his memory:
       But thou, contracted to thine own bright eyes,
       Feed'st thy light's flame with self-substantial fuel,
       Making a famine where abundance lies,
       Thy self thy foe, to thy sweet self too cruel:
       Thou that art now the world's fresh ornament,
       And only herald to the gaudy spring,
       Within thine own bud buriest thy content,
       And tender churl mak'st waste in niggarding:
         Pity the world, or else this glutton be,
         To eat the world's due, by the grave and thee.

Extract the text from exampleSonnets.pdf using extractFileText. The file exampleSonnets.pdf contains Shakespeare's sonnets in a PDF file.

str = extractFileText("exampleSonnets.pdf");

View the second sonnet.

ii = strfind(str,"II");
iii = strfind(str,"III");
start = ii(1);
fin = iii(1);
ans = 
       When forty winters shall besiege thy brow, 
       And dig deep trenches in thy beauty's field, 
       Thy youth's proud livery so gazed on now, 
       Will be a tatter'd weed of small worth held: 
       Then being asked, where all thy beauty lies, 
       Where all the treasure of thy lusty days; 
       To say, within thine own deep sunken eyes, 
       Were an all-eating shame, and thriftless praise. 
       How much more praise deserv'd thy beauty's use, 
       If thou couldst answer 'This fair child of mine 
       Shall sum my count, and make my old excuse,' 
       Proving his beauty by succession thine! 
         This were to be new made when thou art old, 
         And see thy blood warm when thou feel'st it cold. 

Extract the text from pages 3, 5, and 7 of the PDF file.

pages = [3 5 7];
str = extractFileText("exampleSonnets.pdf", ...

View the 10th sonnet.

x = strfind(str,"X");
xi = strfind(str,"XI");
start = x(1);
fin = xi(1);
ans = 
       Is it for fear to wet a widow's eye, 
       That thou consum'st thy self in single life? 
       Ah! if thou issueless shalt hap to die, 
       The world will wail thee like a makeless wife; 
       The world will be thy widow and still weep 
       That thou no form of thee hast left behind, 
       When every private widow well may keep 
       By children's eyes, her husband's shape in mind: 
       Look! what an unthrift in the world doth spend 
       Shifts but his place, for still the world enjoys it; 
       But beauty's waste hath in the world an end, 
       And kept unused the user so destroys it. 
         No love toward others in that bosom sits 
         That on himself such murd'rous shame commits. 
       For shame! deny that thou bear'st love to any, 
       Who for thy self art so unprovident. 
       Grant, if thou wilt, thou art belov'd of many, 
       But that thou none lov'st is most evident: 
       For thou art so possess'd with murderous hate, 
       That 'gainst thy self thou stick'st not to conspire, 
       Seeking that beauteous roof to ruinate 
       Which to repair should be thy chief desire. 

If your text data is contained in multiple files in a folder, then you can import the text data into MATLAB using a file datastore.

Create a file datastore for the example sonnet text files. The examples sonnets have file names "exampleSonnetN.txt", where N is the number of the sonnet. Specify the read function to be extractFileText.

readFcn = @extractFileText;
fds = fileDatastore('exampleSonnet*.txt','ReadFcn',readFcn)
fds = 
  FileDatastore with properties:

                       Files: {
                              ' .../tp395bd4b6/textanalytics-ex73762432/exampleSonnet1.txt';
                              ' .../tp395bd4b6/textanalytics-ex73762432/exampleSonnet2.txt';
                              ' .../tp395bd4b6/textanalytics-ex73762432/exampleSonnet3.txt'
                               ... and 1 more
                 UniformRead: 0
                    ReadMode: 'file'
                   BlockSize: Inf
                  PreviewFcn: @extractFileText
                     ReadFcn: @extractFileText
    AlternateFileSystemRoots: {}

Create an empty bag-of-words model.

bag = bagOfWords
bag = 
  bagOfWords with properties:

          Counts: []
      Vocabulary: [1x0 string]
        NumWords: 0
    NumDocuments: 0

Loop over the files in the datastore and read each file. Tokenize the text in each file and add the document to bag.

while hasdata(fds)
    str = read(fds);
    document = tokenizedDocument(str);
    bag = addDocument(bag,document);

View the updated bag-of-words model.

bag = 
  bagOfWords with properties:

          Counts: [4x276 double]
      Vocabulary: [1x276 string]
        NumWords: 276
    NumDocuments: 4

To extract text data directly from HTML code, use extractHTMLText and specify the HTML code as a string.

code = "<html><body><h1>THE SONNETS</h1><p>by William Shakespeare</p></body></html>";
str = extractHTMLText(code)
str = 
     by William Shakespeare"

Input Arguments

collapse all

Name of the file, specified as a string scalar or character vector.

Data Types: string | char

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Pages',[1 3 5] specifies to read pages 1, 3, and 5 from a PDF file.

Character encoding to use, specified as the comma-separated pair consisting of 'Encoding' and a character vector or a string scalar. The character vector or string scalar must contain a standard character encoding scheme name such as the following.











































If you do not specify an encoding scheme, then the function performs heuristic auto-detection for the encoding to use. If these heuristics fail, then you must specify one explicitly.

This option only applies when the input is a plain text file.

Data Types: char | string

Extraction method, specified as the comma-separated pair consisting of 'ExtractionMethod' and one of the following:

'tree'Analyze the DOM tree and text contents, then extract a block of paragraphs.
'article'Detect article text and extract a block of paragraphs.
'all-text'Extract all text in the HTML body, except for scripts and CSS styles.

Password to open PDF file, specified as the comma-separated pair consisting of 'Password' and a character vector or a string scalar. This option only applies if the input file is a PDF.

Example: 'Password','skroWhtaM'

Data Types: char | string

Pages to read from PDF file, specified as the comma-separated pair consisting of 'Pages' and a vector of positive integers. This option only applies if the input file is a PDF file. The function, by default, reads all pages from the PDF file.

Example: 'Pages',[1 3 5]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64


Introduced in R2017b