How to loop multi-line with data text?

1 view (last 30 days)
Peter Fang
Peter Fang on 20 Apr 2022
Commented: Peter Fang on 21 Apr 2022
Hi everyone.
I have a question: How to loop muilti-line with data text? (The image describes the data below)
Please help me.
Thank you.
  5 Comments
Peter Fang
Peter Fang on 20 Apr 2022
The reason i want to check them one by one is because, i need to find the word Printing Done and from which date it appears.
But also thank you very much for your answer :D

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 20 Apr 2022
hello again
try this code - does not require readlines
line_index will give you which line contains the searched string
filename = 'EX.txt';
str = "Printing Done";
[lines,count,line_index] = myfunction_read(filename,str)
%%%%%%% functions %%%%%%%%%
function lines = my_readlines(filename)
% work around for earlier matlab releases (not having readlines)
lines = regexp(fileread(filename), '\r?\n', 'split');
if isempty(lines{end}); lines(end) = []; end %end of file correction
end
%%%%%%%%%%%%%%%%%%%%%%%%%
function [lines,count,line_index] = myfunction_read(filename,str)
lines = my_readlines(filename);
% init data
count = 0;
for ci = 1:numel(lines)
ll = lines(ci);
if contains(ll,str) %
count = count+1;
line_index(count) = ci;
end
end
end
  1 Comment
Mathieu NOE
Mathieu NOE on 20 Apr 2022
if you need to display the corresponding lines, it's fairly easy :
selected_lines = lines(line_index)'
selected_lines =
5×1 cell array
{'2021-3-31 7:23:46 504 [INFO] Print Job 6704654239889586 Status Done: Printing Done'}
{'2021-3-31 7:24:3 486 [INFO] Print Job 6704654422370495 Status Done: Printing Done' }
{'2021-3-31 7:25:6 409 [INFO] Print Job 6704655051285743 Status Done: Printing Done' }
{'2021-3-31 7:27:7 256 [INFO] Print Job 6704656259440377 Status Done: Printing Done' }
{'2021-3-31 7:27:18 300 [INFO] Print Job 6704656372070258 Status Done: Printing Done'}

Sign in to comment.

More Answers (1)

Voss
Voss on 20 Apr 2022
You can read the file in one fell swoop, then break the contents into individual lines, and retrieve the dates/times from lines where 'Printing Done' appears:
% read the entire file
fid = fopen('EX.txt');
data = fread(fid,'*char').';
fclose(fid);
% cell array C, with each element containing one line of text
C = strsplit(data,newline()).'
C = 53×1 cell array
{'2021-3-31 5:2:56 258 [INFO] Listen Port 104 Success←' } {'2021-3-31 7:23:44 20 [INFO] Print Job 6704654239889586 Status Pending: Preparing films for printing←' } {'2021-3-31 7:23:44 145 [INFO] Print Job 6704654239889586 Status Printing: PrinterIP=192.168.121.103 AETitle=DXWORKS Printing Started←' } {'2021-3-31 7:23:44 426 [INFO] 【Printer Array】 Printer CANON ADHR16810 10X12INCH OUTDUOI Jobs 0←' } {'2021-3-31 7:23:44 442 [INFO] 【Printer Array】 Printer CANON ADHR18526 10X12INCH OUTTREN Jobs 0←' } {'2021-3-31 7:23:44 457 [INFO] 【Printer Array】 Printer CANON ADHR19500 10X12INCH Jobs 0←' } {'2021-3-31 7:23:44 473 [INFO] 【Printer Array】 Printer CANON ADHR19965 10X12INCH OUTGIUA Jobs 0←' } {'2021-3-31 7:23:44 489 [INFO] 【Printer Array】 Printer CANON ADHR24739 10X12 DUOI TRONG Jobs 0←' } {'2021-3-31 7:23:44 551 [INFO] 【Printer Array】 Printer CANON ADHR16810 10X12INCH OUTDUOI Start printing!←' } {'2021-3-31 7:23:45 410 [INFO] Print Job 6704654239889586 Status Printing: Printing film 1 of 1←' } {'2021-3-31 7:23:46 504 [INFO] Print Job 6704654239889586 Status Done: Printing Done←' } {'2021-3-31 7:23:46 504 [INFO] Print complete! Filmsize:10INX12IN PrintType:GrayScale Printer: CANON ADHR16810 10X12INCH OUTDUOI Paper: 10"x12" 25x30cm ←'} {'2021-3-31 7:24:2 237 [INFO] Print Job 6704654422370495 Status Pending: Preparing films for printing←' } {'2021-3-31 7:24:2 252 [INFO] Print Job 6704654422370495 Status Printing: PrinterIP=192.168.121.103 AETitle=DXWORKS Printing Started←' } {'2021-3-31 7:24:2 299 [INFO] 【Printer Array】 Printer CANON ADHR16810 10X12INCH OUTDUOI Jobs 1←' } {'2021-3-31 7:24:2 315 [INFO] 【Printer Array】 Printer CANON ADHR18526 10X12INCH OUTTREN Jobs 0←' } {'2021-3-31 7:24:2 346 [INFO] 【Printer Array】 Printer CANON ADHR19500 10X12INCH Jobs 0←' } {'2021-3-31 7:24:2 362 [INFO] 【Printer Array】 Printer CANON ADHR19965 10X12INCH OUTGIUA Jobs 0←' } {'2021-3-31 7:24:2 393 [INFO] 【Printer Array】 Printer CANON ADHR24739 10X12 DUOI TRONG Jobs 0←' } {'2021-3-31 7:24:2 440 [INFO] 【Printer Array】 Printer CANON ADHR18526 10X12INCH OUTTREN Start printing!←' } {'2021-3-31 7:24:2 971 [INFO] Print Job 6704654422370495 Status Printing: Printing film 1 of 1←' } {'2021-3-31 7:24:3 486 [INFO] Print Job 6704654422370495 Status Done: Printing Done←' } {'2021-3-31 7:24:3 486 [INFO] Print complete! Filmsize:10INX12IN PrintType:GrayScale Printer: CANON ADHR18526 10X12INCH OUTTREN Paper: 10"x12" 25x30cm ←' } {'2021-3-31 7:25:5 128 [INFO] Print Job 6704655051285743 Status Pending: Preparing films for printing←' } {'2021-3-31 7:25:5 159 [INFO] Print Job 6704655051285743 Status Printing: PrinterIP=192.168.121.103 AETitle=DXWORKS Printing Started←' } {'2021-3-31 7:25:5 206 [INFO] 【Printer Array】 Printer CANON ADHR16810 10X12INCH OUTDUOI Jobs 1←' } {'2021-3-31 7:25:5 222 [INFO] 【Printer Array】 Printer CANON ADHR18526 10X12INCH OUTTREN Jobs 1←' } {'2021-3-31 7:25:5 253 [INFO] 【Printer Array】 Printer CANON ADHR19500 10X12INCH Jobs 0←' } {'2021-3-31 7:25:5 284 [INFO] 【Printer Array】 Printer CANON ADHR19965 10X12INCH OUTGIUA Jobs 0←' } {'2021-3-31 7:25:5 300 [INFO] 【Printer Array】 Printer CANON ADHR24739 10X12 DUOI TRONG Jobs 0←' }
% keep only lines saying 'Printing Done'
C = C(contains(C,'Printing Done'))
C = 5×1 cell array
{'2021-3-31 7:23:46 504 [INFO] Print Job 6704654239889586 Status Done: Printing Done←'} {'2021-3-31 7:24:3 486 [INFO] Print Job 6704654422370495 Status Done: Printing Done←' } {'2021-3-31 7:25:6 409 [INFO] Print Job 6704655051285743 Status Done: Printing Done←' } {'2021-3-31 7:27:7 256 [INFO] Print Job 6704656259440377 Status Done: Printing Done←' } {'2021-3-31 7:27:18 300 [INFO] Print Job 6704656372070258 Status Done: Printing Done' }
% grab the dates/times from those lines
dates = regexp(C,'(\d+-\d+-\d+ \d+:\d+:\d+)','tokens','once');
dates = vertcat(dates{:})
dates = 5×1 cell array
{'2021-3-31 7:23:46'} {'2021-3-31 7:24:3' } {'2021-3-31 7:25:6' } {'2021-3-31 7:27:7' } {'2021-3-31 7:27:18'}
  3 Comments

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!