How should I write a code that can realize if a text file is updated and the code updates my plot too?

I have a log file which is keep updating a message (text file attached). This message can somehow be plotted and I have done that (see below) but I don't know how to update my plot when the txt file is updated with a new line?
logfile = uigetfile
opt = detectImportOptions(logfile,'FileType','text','Delimiter',','); % setting import options
opt.VariableNames = {'LogType','RealTimeStamp','DataSet','ParticleTimeStamp','ForcedParticleFlag','TriggerIntensity','Channel2','Channel3','Channel4','Channel5','Channel6','Channel7','Channel8','Channel9','Channel10','Channel11','Channel12','Channel13','Channel14','Channel15','Channel16','Channel17','Channel18','Channel19','Channel20','Channel21','Channel22','Channel23','Channel24','Channel25','Channel26','Channel27','Channel28','Channel29','Channel30','Channel31','Channel32'};
opt.ExtraColumnsRule ='ignore'; % remove extra column
ImportLog = readtable(logfile,opt); % import log file
ImportLog=ImportLog(~any(ismissing(ImportLog),2),:); % import only scattering data and remove other data
ChannelToAngle_T = PHIPS_ChannelToAngle(ImportLog); % channel to angle table
ForcedTrigger = ChannelToAngle_T.ForcedParticleFlag; % forced trigger out of table
idx0 = find(ForcedTrigger == 0); % index of those lines that has forced trigger or not
idx1 = find(ForcedTrigger == 1);
for i= 1:numel(idx0)
Angle = 18:8:170; % Angles
ASF = table2array(ChannelToAngle_T(idx0(end), end-19:end)); % Angular Scattering Function
semilogy(Angle, ASF, 's-','MarkerFaceColor', 'w', 'LineWidth', 2, 'MarkerSize', 10) % plot
hold on
%semilogy(angle, ASF_2, 's--','MarkerFaceColor', 'w', 'LineWidth', 2, 'MarkerSize', 10)
%semilogy(angle, ASF_3, 's--','MarkerFaceColor', 'w', 'LineWidth', 2, 'MarkerSize', 10)
hold off
grid on
xlabel ('Scattering Angle')
ylabel ('Scattering Intensity [cts]')
ylim([0, 5000])
ax = gca;
ax.YAxis(1).Color = 'k'; % change color of LHS y-axis to black
% ax.YAxis(2).Color = 'k'; % change color of RHS y-axis to black
ax.FontSize = 14;
ax.FontWeight = 'bold';
legend('Location', 'southeast')
%
end

 Accepted Answer

You could periodically check the date of the file with dir() (in a for loop or with a timer) and if the file date changed from the last time you checked it, update your plot.

8 Comments

Thank you. I decided to use the dir() and
size0 = FileInfo.bytes
to compare my files. and how should I put the plot in the loop that it updates. This part of the logic I cannot understand. I want it to just rewrite the same plot over and over again. Something like below:
if size1>=size0
while something
????
end
end
If you are updating everything, then just cla() and redraw everything.
If you want to just update one particular line without disturbing anything else, then I recommend you look at animatedline()
if size1>=size0
You have the problem that the fact that a file has increased in size does not mean that the new last line in the file is complete. Unless the program is using line buffering (flushing after every line), then it might be in the middle of writing something out at the time you check.
At the moment I cannot tell whether you are using Windows or Mac or Linux. If you are using Windows then there is the additional risk that the file might be locked .
The lines are sent via a Windows console as a UDP message which I think (not sure) would be a packet of data but I am not completely sure how it's stored as a log file. I need to ask our software engineer. I am actually using this in a Matlab App so that is why I am a little confused how to put this plot in a while loop. I need to use semilogy to demonstrate this data. I will use the timer instead but can you please explain this while loop I wanna put in matlab app?
how_long_to_wait = 5;
[logfile, logdir] = uigetfile;
if ~ischar(logfile); return; end %user cancel
logfile = fullfile(logdir, logfile);
opt = detectImportOptions(logfile,'FileType','text','Delimiter',','); % setting import options
opt.VariableNames = {'LogType','RealTimeStamp','DataSet','ParticleTimeStamp','ForcedParticleFlag','TriggerIntensity','Channel2','Channel3','Channel4','Channel5','Channel6','Channel7','Channel8','Channel9','Channel10','Channel11','Channel12','Channel13','Channel14','Channel15','Channel16','Channel17','Channel18','Channel19','Channel20','Channel21','Channel22','Channel23','Channel24','Channel25','Channel26','Channel27','Channel28','Channel29','Channel30','Channel31','Channel32'};
opt.ExtraColumnsRule ='ignore'; % remove extra column
ax = app.axes1;
cla(ax)
ax.YScale = 'log';
h = plot(nan, nan, 's-','MarkerFaceColor', 'w', 'LineWidth', 2, 'MarkerSize', 10);
oldsize = -inf;
while true
FileInfo = dir(logfile);
if FileInfo.bytes > oldsize
oldsize = FileInfo.bytes;
ImportLog = readtable(logfile,opt); % import log file
do some stuff
h.XData = Angles; h.YData = ASF;
drawnow();
else
pause(how_long_to_wait)
end
end
Does it always increase in size? If not then you could replace
if FileInfo.bytes > oldsize
with
if FileInfo.bytes ~= oldsize

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!