Santosh M.
Santosh M.

Reputation: 2454

Read a text with certain pattern in CSV file

I have Excel CSV file which has some text and data. In CSV file, for example, I have statements, as following (I have many statements and data after each statement. The following 2 statements is just an example):

1_Q6 - Walmart (https://www.glassdoor.com)
5_Q5 - Phillips 66 (https://www.glassdoor.com)

So, I wish to read words only "Walmart" from the first statement in the first row and "Phillips 66" from the second statement in the second row. As you can see, the location of both these word have a pattern. It is succeeded by a hyphen and blank space. It has pattern like following:

_Qnumber - "The word I am interested in" (

So, to summarize, I wish to read the word that is succeeded by "_Qnumber -" and preceded by " (" . That "number" is always going to be 5 or 6.

I hope you get what I am trying to achieve. I have already tried using xlsread and textscan but no success.

Thanks a lot for your help in advanced.

Upvotes: 0

Views: 32

Answers (2)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22215

Textscan works well for me:

>> type textscan.dat % show contents of file
1_Q6 - Walmart (https://www.glassdoor.com)
5_Q5 - Phillips 66 (https://www.glassdoor.com)

 

>> f = fopen ('textscan.dat', 'r');        
>> C = textscan (f, '%*u_%*c%u - %s %*[^\n]')
C = 
    [2x1 uint32]    {2x1 cell}
>> C{:}
ans =
           6
           5
ans = 
    'Walmart'
    'Phillips'

PS. Don't forget that if you need to re-read f, you'll have to frewind(f) first. Also, don't forget to fclose(f).

Upvotes: 1

tanaydin
tanaydin

Reputation: 5306

http://www.mathworks.com/help/matlab/ref/regexp.html

These are the functions that you are looking for.

Upvotes: 0

Related Questions