How can I use regexp to total the amount of lines containing a certain word.

2 views (last 30 days)
I have a file with multiple lines ending with the words "valid" or "corrupted" and I am attempting to calculate the total number of lines that contain the word "valid". I have been trying to do so using regexp but have currently only been able to receive 0 as the amount of lines that contain this word. How can I use regexp to find all the lines with valid at the end?
  3 Comments
Sean de Wolski
Sean de Wolski on 19 Nov 2013
By multiple lines, do you mean an nx1 cell array, an mxn char array or an mx1 char array with '\n's?
Kevin
Kevin on 19 Nov 2013
while ~feof(fid)
validline = fgetl(fid);
logical(validline);
if regexp(validline, '(valid)', 'match');
end
valid{end+1,1} = validline;
end
fprintf('Number of valid lines: %d %n', numel(valid)
This is what I attempted to do, however I am quite certain that I've got it wrong. The the lines of code come from a file formatted like this
52.9-24.6-25.3-89.2-13.5 valid
43.6-69.4-25.6-69.5 corrupted

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 20 Nov 2013
while true
validline = fgetl(fid);
if ~ischar(validline); break; end
if regexp(validline, '(valid)', 'match');
valid{end+1,1} = validline;
end
end
fprintf('Number of valid lines: %d %n', numel(valid))
  5 Comments
Kevin
Kevin on 20 Nov 2013
Thank you that was able to return the correct answer. The previous code returned values of 0 and 1 because I had it set up as a logical.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!