function [ line ] = FindLineNumber( fid, fileSize, lineNum ) %FindLineNumber Find a line number in file where each line starts with the %number % Detailed explanation goes here currPos = floor(fileSize / 2); minPos = 0; maxPos = fileSize; while(1) %fprintf('searching for %d, currPos %d, minPos %d, maxPos %d\n', lineNum, currPos, minPos, maxPos); res = fseek(fid, currPos, 'bof'); line = fgetl(fid); if currPos > 0 line = fgetl(fid); if line == -1 fprintf('line == -1'); currNode = -1; break; end end %split the line into a vector of node numbers r = str2double(regexp(line, ' ', 'split')); %r(2) = []; %the second number is a timestamp currNode = r(1); %the first number is the node and the following numbers are the neighbor indexes %if we are close to the node, start linear search if lineNum >= currNode && lineNum - currNode < 10 while currNode < lineNum line = fgetl(fid); r = str2double(regexp(line, ' ', 'split')); currNode = r(1); %the first number is the node and the following numbers are the neighbor indexes end break; %otherwise continue with binary search else if currNode > lineNum maxPos = currPos; currPos = floor((currPos + minPos) / 2); else minPos = currPos; currPos = floor((currPos + maxPos) / 2); end end end %fprintf('searched for %d, found %d\n', lineNum, currNode); end