-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremoveLinesWithStrings.m
39 lines (35 loc) · 1.24 KB
/
removeLinesWithStrings.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
function removeLinesWithStrings(inputFile, outputFile, stringList)
% Open the input file for reading
fidInput = fopen(inputFile, 'r');
if fidInput == -1
error('Cannot open input file: %s', inputFile);
end
% Open the output file for writing
fidOutput = fopen(outputFile, 'w');
if fidOutput == -1
error('Cannot open output file: %s', outputFile);
end
try
% Read and process the file line by line
while ~feof(fidInput)
line = fgets(fidInput); % Read a line from the input file
if isempty(line)
continue; % Skip empty lines
end
% Check if the first three characters match any of the strings in the list
lineStart = strtrim(line(1:min(3, end))); % Get the first three characters
if ~any(strcmp(lineStart, stringList))
% If no match, write the line to the output file
fprintf(fidOutput, '%s', line);
end
end
catch ME
% If an error occurs, close the files and rethrow the error
fclose(fidInput);
fclose(fidOutput);
rethrow(ME);
end
% Close the files
fclose(fidInput);
fclose(fidOutput);
end