Importing data from .txt, by last symbol in line.

1 view (last 30 days)
I need to import data from at .txt file, with 10-15 lines set-up like this and some gibberish lines that needs to be ignored.
[Question] [Answer as T or F]
So every line i need to use will end with either a T or an F, and i am trying to figure out the best way to only find the lines that end with T or F.
I need the questions in one array and the answers in another.
I've tried using textscan, but i cant get it to ignore lines that doesnt end on either T or F.
  1 Comment
dpb
dpb on 26 Apr 2014
Edited: dpb on 27 Apr 2014
That would be a regexp kind of parsing but I'm not adept enough to write it otomh w/o more effort than care to spend at the moment.
I'd probably just read the whole file as cell array, then throw away the unwanted lines from memory and process the rest, also in memory.

Sign in to comment.

Accepted Answer

dpb
dpb on 27 Apr 2014
Edited: dpb on 27 Apr 2014
Or, since it would seem such a file can't be very large, just use fgetl on a line-by-line basis...sotoo
fid=fopen('filename','r');
while ~feof(fid)
l=fgetl(fid);
if l(end)=='T' || l(end)=='F'
% parse l for Q and A here
end
end
fid=fclose(fid);

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!