forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findLineInFile.m
61 lines (55 loc) · 1.45 KB
/
findLineInFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [ out, matchIndx ] = findLineInFile( file, exp, varargin )
% [ outLine, matchIndx ] = findLineInFile( file, exp [, 'nLines', nLines ] )
%
% Finds the first line in the file that matches expression
%
% Inputs:
% file - a string with the file to open
% exp - the experession to match
%
% Optional Inputs:
% nLines - the number of lines to acquire
%
% Outputs:
% outLine - the line that matched expression
%
% Optional Outputs:
% matchIndx - the index of the line where exp was matched
%
% Written by Nicholas Dwork - Copyright 2019
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
p = inputParser;
p.addParameter( 'nLines', 1, @ispositive );
p.parse( varargin{:} );
nLines = p.Results.nLines;
fp = fopen( file, 'r' );
if nLines > 1
out = cell( nLines, 1 );
else
out = [];
end
while feof( fp ) == 0
thisLine = fgetl( fp );
matchIndx = regexp( thisLine, exp );
if matchIndx
if nLines > 1
out{1} = thisLine;
for lineIndx = 2:nLines
if feof( fp ), break; end
thisLine = fgetl( fp );
out{ lineIndx } = thisLine;
end
else
out = thisLine;
end
break;
end
end
fclose( fp );
end