Read a textfile in MATLAB
이전 댓글 표시
I am using MATLAB to call another application which generates a huge text file of the results I want. I'm looking for a way to scan for lines which contain specific strings, retrieve specific numeric values from those lines and store them in a numeric array.
I've been using the following code:
fid = fopen('filename.txt');
var1 = textscan(fid,'%s','delimiter','\n');
lines = var1{1,1};
Since textscan creates a cell which has the whole text file as the first entry in that cell, I declare lines as a secondary variable to get get the text in separate lines.
Thereafter, I use the following code:
if strfind(lines{i}, 'String Sequence')== 1
line2 = char(lines{i});
This converts the lines{i} cell to a string. I need to retrieve specific numeric values from this string:
1. I know the position where the values I need are located in this string. 2. I know the string sequence which precedes these values.
For example the string retrieved could read:
line2 =
The fare for a 7% ROI = 343.24 DOLLARS/PASS
For multiple runs, the structure of the string remains the same, only the numeric value - 343.24 in this case - varies. Can someone suggest a way to implement the seach logic described above.
My program generates a text file with some 20,000 lines at each optimization run. Is there a better (faster) way of implementing the text search?
댓글 수: 4
dpb
2013년 10월 1일
Are the number of lines known/constant?
Is this the only line you care about?
Gautam Marwaha
2013년 10월 1일
Matt Kindig
2013년 10월 1일
Can you clarify what data you are trying to extract? Just the number in front of DOLLARS/PASS (as Cedric's answer below), or additional information?
Gautam Marwaha
2013년 10월 1일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!