Help with Matlab homework quesion

2 views (last 30 days)
Peter Fang
Peter Fang on 19 Apr 2022
Answered: Eric Delgado on 19 Sep 2022
Can someone help me with this homework? I'm extremely new to MATLAB and have no idea how to do this.
The log.txt file is created from the DICOM film printing software, to record the software's activities.
- Each line corresponds to an event occurring on the system. The data fields are separated by "space", in order:
<date> <time> <event code> <event type> <event details>
- By counting the event lines with the phrase "Printing Done" in the event details, we can calculate the number of printed films.
P/s: I would like to summarize is: ask to count the lines with 'Printing Done', and in 1 day how many lines appear 'Printing Done'. Then output that count data to a txt file.
I have attached 1 .txt file below, because the file is too big, I extracted it.
Please help me.
Thank you.

Answers (2)

Image Analyst
Image Analyst on 19 Apr 2022
Try this:
fileContents = fileread('EX.txt');
locations = strfind(fileContents, 'Printing Done')
locations = 1×5
1024 2152 3271 4387 5270
count = length(locations)
count = 5
Then call writematrix().

Eric Delgado
Eric Delgado on 19 Sep 2022
Hi Peter, there are a lot of ways to do the same thing. I wrote in Matlab R2021b one of those ways... and I can't avoid commenting what an ugly struct of the log file. :)
Hope it's helpful!
% STEP 1: read table info
opts.VariableNames = ["date", "time", "code", "type"];
opts.VariableTypes = ["datetime", "datetime", "double", "categorical"];
T = readtable('EX.txt', opts);
for ii = 1:height(T)
T.description{ii} = deblank(strjoin(T{ii,5:17}));
end
T(:,5:17) = [];
head(T)
% STEP 2: desirable outputs
% Number of lines with 'Printing Done' info
output1 = sum(contains(T.description, 'Printing Done'))
% Map between the number of lines with 'Printing Done' and days
output2 = table('Size', [0, 2], 'VariableTypes', {'datetime', 'double'}, 'VariableNames', {'date', 'count'});
timestamps = unique(T.date);
for ii = 1:numel(timestamps)
output2(end+1,:) = {timestamps(ii), numel(intersect(find(T.date == timestamps(ii)), find(contains(T.description, 'Printing Done'))))};
end
output2
% STEP3: create files with the outputs (the second is awesome with the Pretty
% Print json!)
writematrix(output1, 'EX_output1.txt')
writematrix(jsonencode(output2, 'PrettyPrint', true), 'EX_output2_json.txt')

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!