Laurengineer
Laurengineer

Reputation: 747

Search text file in MATLAB for a string to begin process

I have a fairly large text file and am trying to search for a particular term so that i can start a process after that point, but this doesn't seem to be working for me:

fileID = fopen(resfile,'r');


line  = 0;
    while 1
    tline = fgetl(fileID);
    line = line + 1;
    if ischar(tline)
        startRow = strfind(tline, 'OptimetricsResult');
        if isfinite(startRow) == 1;
            break
        end
    end
end

The answer I get is 9, but my text file:

$begin '$base_index$'
$begin 'properties'
all_levels=000000000000
time(year=000000002013, month=000000000006, day=000000000020, hour=000000000008, min=000000000033, sec=000000000033)
version=000000000000
$end 'properties'
$begin '$base_index$'
$index$(pos=000000492036, lin=000000009689, lvl=000000000000)
$end '$base_index$'

definitely doesn't have that in the first 9 rows?

If I ctrl+F the file, I know that OptimetricsResult only appears once, and that it's 6792 lines down

Any suggestions?

Thanks

Upvotes: 1

Views: 9564

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

I can't reproduce your problem.

Are you sure you've properly closed the file before re-running this script? If not, the internal line counter in fgetl does not get reset, so you get false results. Just issue a fclose all on the MATLAB command prompt, and add a fclose(fileID); after the loop, and test again.

In any case, I suggest modifying your infinite-loop (with all sorts of pitfalls) to the following finite loop:

haystack = fopen(resfile,'r');
needle   = 'OptimetricsResult';

line  = 0;
found = false;
while ~feof(haystack)

    tline = fgetl(haystack);
    line = line + 1;

    if ischar(tline) && ~isempty(strfind(tline, needle))
        found = true;
        break;
    end

end

if ~found
    line = NaN; end

fclose(fileID);


line

You could of course also leave the searching to more specialized tools, which come free with most operating systems:

haystack = 'resfile.txt';
needle   = 'OptimetricsResult';

if ispc % Windows
    [~,lines] = system(['find /n "' needle '" ' haystack]);
elseif isunix % Mac, Linux
    [~,lines] = system(['grep -n "' needle '" ' haystack]);
else 
    error('Unknown operating system!');
end

You'd have to do a bit more parsing to extract the line number from C, but I trust this will be no issue.

Upvotes: 2

Eitan T
Eitan T

Reputation: 32930

I think your script somehow works, and you were just looking at the wrong variable. I assume that the answer you get is startRow = 9 and not line = 9. Check the variable line. By the way, note that you're not checking an End-of-File, so your while loop might run indefinitely the file doesn't contain your search string.

An alternative approach, (which is much simpler in my humble opinion) would be reading all lines at once (each one stored as a separate string) with textscan, and then applying regexp or strfind:

%// Read lines from input file
fid = fopen(filename, 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);

%// Search a specific string and find all rows containing matches
C = strfind(C{1}, 'OptimetricsResult');
rows = find(~cellfun('isempty', C));

Upvotes: 9

Related Questions