Extract text data and time data from notepad file
2 views (last 30 days)
Show older comments
I have a very large text file in notepad and I need to extract data from it.
The exact data is displayed as such:
05/12/2011 - 14:39:38.790329 - INFO_STATUS HEX CODE 001c DECIMAL CODE 28 ERROR NAME Overflow Error DESCRIPTION Information overflow error occurred.
05/12/2011 - 14:39:39.910752 - INFO_STATUS HEX CODE 001c DECIMAL CODE 28 ERROR NAME Overflow Error DESCRIPTION Information overflow error occurred.
05/12/2011 - 14:39:41.030363 - CP_PROCESSING_STATUS HEX CODE 019c DECIMAL CODE 412 ERROR NAME Computer processing error DESCRIPTION Computer 2 experienced a commanded test error due to a processing error only
05/12/2011 - 14:39:42.150375 - INFO_STATUS HEX CODE 001c DECIMAL CODE 28 ERROR NAME Overflow Error DESCRIPTION Information overflow error occurred.
This is just a segment of the data from the actual file. There are actually many groups of lines of data like this.
What I want to do is read the file into matlab and write the data into columns that says error name, time, description, and hex code.
Any help is much appreciated. V/R, Charles Atlas
4 Comments
Oleg Komarov
on 18 Jul 2011
And then it's followed directly with no breaks in between by another error message, right?
Do you want the date as a string or a numeric serial date (precision up to milliseconds only)?
Answers (2)
Oleg Komarov
on 18 Jul 2011
fid = fopen('test.txt');
out = textscan(fid,'%f/%f/%f - %f:%f:%f - %s','Delimiter','','CollectOutput',1);
time = datenum(out{1}(:,[3:-1:1 4:end]));
info = regexpi(out{2},'CODE (\w+)[\w\s]+NAME ([\w\s]+) DESCRIPTION ([\w\s]+)','tokens');
info = cat(1,info{:}); info = cat(1,info{:});
fid = fclose(fid);
If you need the time to remain in string format replace then:
fid = fopen('test.txt');
out = textscan(fid,'%10s - %15s - %s','Delimiter','','CollectOutput',1);
out{1}(:,3) = regexpi(out{1}(:,3),'CODE (\w+)[\w\s]+NAME ([\w\s]+) DESCRIPTION ([\w\s]+)','tokens');
out{1}(:,3) = cat(1,out{1}{:,3}); out{1}(:,3:5) = cat(1,out{1}{:,3});
fid = fclose(fid);
EDIT
fid = fopen('test.txt');
fmt = '%f/%f/%f-%f:%f:%f%*[^\n]\n HEX CODE %s\r\n %*[^\n]\n ERROR NAME %s\r\n DESCRIPTION%s';
out = textscan(fid,fmt,'Delimiter','','CollectOutput',1);
out{1} = datenum(out{1}(:,[3:-1:1 4:end]));
fid = fclose(fid);
0 Comments
Walter Roberson
on 18 Jul 2011
If it is a large enough file or performance is important, use perl()
0 Comments
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!