Question: MATLAB Write a function with the header: function [words] = myText2Cells(fp, delimVec) which takes a file pointer and a delimiter vector (delimVec = [',.?=;:/- ()'];)

MATLAB

Write a function with the header: function [words] = myText2Cells(fp, delimVec) which takes a file pointer and a delimiter vector (delimVec = [',.?=;:/- "()'];) to parse a text file into a cell-array of words.

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

2. 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.)

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

4. 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).

5. Store each token as an element of a cell array.

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!