How do i find one string with another?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi Guys,
I am trying to sequentially look for a string in a document and was wondering how I would go about doing that.
Essentially I have a large file called A.csv with a bunch of columns [date], [Open], [Low], [Close], [Volume], [Adj.Close], [Ret]
I want to write a script that will find a date 4/5/2000 and will pull the corresponding return for that date.
This is the trick: the day is a variable. All of the dates have a different month and year so it looks something like 1/3/2000, 2/4/2000, 3/1/2000. How do I find a match using the year and month. For example, I want to pull 1/*/2006, 7/*/2007, but I don't know what the * is (it could be 1, 2, 3, 4, 5, etc...)
The first row for example (skipping the header), looks like: 1/3/2000,78.75,78.94,58.13,66.19,1642300,62.37,0.569183903
Thank you for all of your help guys!
-Larry G.
채택된 답변
Andrew Newell
2012년 1월 11일
The trick is to use regular expressions. The first line below searches for any string that has one or more integers between '1/' and '/2000'. One line at a time is examined and the number extracted if there is a match.
match_str = '1/[0-9]+/2000';
match_vector = zeros(32000,1); % Use whatever size you're sure is large enough
fid=fopen('yourfile.m');
count=0;
tline = fgetl(fid);
while ischar(tline)
if regexp(tline,match_str)
A = textscan(tline,'%*s %*f %*f %*f %*f %*d %*f %f','delimiter',',');
count = count+1;
match_vector(count) = A{1};
end
end
fclose(fid);
댓글 수: 5
Andrew Newell
2012년 1월 11일
Ah - I found the error: I should be updating match_vector inside the IF-END block.
추가 답변 (2개)
Walter Roberson
2012년 1월 11일
regexp(STRING, '^(?<=1/\d+/2006/.*,)[^,]+$', 'match', 'dotexceptnewline', 'lineanchors')
댓글 수: 0
Laurentiu Galan
2012년 1월 11일
댓글 수: 1
Andrew Newell
2012년 1월 11일
I don't see anything obvious. For any code I suggest the following sequence: (1) test it thoroughly to make sure it works; (2) run it with the MATLAB Profiler and see where the code is spending most of its time; and (3) look for ways to speed up that part of the code.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!