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

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

By multiple lines, do you mean an nx1 cell array, an mxn char array or an mx1 char array with '\n's?
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)

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

still returns the value of 0 when used
Try using 'valid' instead of '(valid)' -- though in theory that should not matter in a regular expression.
It still does not read the correct number of lines, is it possible that there is another way to go about extracting all of the valid lines?
FileContents = fileread('TheFileName.txt');
count = length(regexp(FileContents, 'valid'));
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.

Categories

Products

Tags

Asked:

on 19 Nov 2013

Commented:

on 20 Nov 2013

Community Treasure Hunt

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

Start Hunting!