Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function with the header: function [words] = myText2Cells(fp, delimVec) which takes a file pointer and a delimiter vector (given in the test case)

Write a function with the header:

 

function [words] = myText2Cells(fp, delimVec)

 

which takes a file pointer and a delimiter vector (given in the test case) to parse a text file into a cell-array of words. 

HINTS:

Use fgetl to read the first line of the file pointed to by fp.

Use the following while loop:

while(~feof(fp))

which will keep reading the text file pointed to by fp as long as the "end-of-file" indicator has not been encountered. (Every time you read something from a file, Matlab tests that "something" to see if it is a hidden character called the "end-of-file" character.)

Review strtok .  It takes two arguments, a string and one or more delimiters.  It breaks the string into everything before the first delimiter and everything after the first delmiter.  (A delimiter is a character which is used to separate tokens in a string.  Commas and whitespace are common delmiters.)

 

SAMPLE CODE (modified from the Matlab strtrok help file):

remain = 'All work and no play makes Homer something something';

while ~isempty(remain)

   [token, remain] = strtok(remain, ' ')

end

 

Create ANOTHER WHILE LOOP inside the above while loop which keeps "tokenizing" a line of text with strtok as long as remain is not empty (as in the sample code above).

Store each token as an element of a cell array.

Check each token to ensure it is not empty. If it is, don't store it in your cell array.

 

Test Case

DeepWork.txt has been provided on the shared drive in the folder called "HW10". Copy it to your computer and add it somewhere to your Matlab path.  Copy the delimVec from the text below.

 

>> fp = fopen('DeepWork.txt', 'r');

>> delimVec = [',.?=;:‘’/–-— "“”()'];

>> A = myText2Cells(fp1, delimVec);

>> A

 

A =

 

  1×2648 cell array

 

  Columns 1 through 6

 

    {'INTRODUCTION'}   {'Who'}    {'is'}   {'this'}    {'book'}    {'for'}

 

  Columns 7 through 12

 

    {'Cal'}   {'Newport'}    {'s'}   {'Deep'}    {'Work'}    {'is'}

 

  Columns 13 through 18

 

    {'a'}   {'guide'}    {'on'}   {'how'}    {'to'}    {'have'}

 

  Columns 19 through 23

 

    {'focused'}   {'success'}    {'in'}   {'a'}    {'distracted'}

 

  Columns 24 through 29

 

    {'world'}   {'This'}    {'book'}   {'is'}    {'a'}    {'great'}

 

  Columns 30 through 35

 

    {'read'}   {'for'}    {'anyone'}   {'looking'}    {'to'}    {'get'}

(rest of the output is not shown but my A has 2648 elements)

Step by Step Solution

3.40 Rating (150 Votes )

There are 3 Steps involved in it

Step: 1

answer function words myText2Cellsfp delimVec words whilefeoffp ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Document Format ( 2 attachments)

PDF file Icon
635d68bbb3aed_175319.pdf

180 KBs PDF File

Word file Icon
635d68bbb3aed_175319.docx

120 KBs Word File

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Calculus Early Transcendentals

Authors: James Stewart

7th edition

538497904, 978-0538497909

More Books

Students also viewed these Accounting questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago