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 =

12648 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

There are 3 Steps involved in it

Step: 1

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_2

Step: 3

blur-text-image_3

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago