How should I write a code that can realize if a text file is updated and the code updates my plot too?
    8 views (last 30 days)
  
       Show older comments
    
    Sepehr Ariaei
 on 26 Jun 2021
  
    
    
    
    
    Answered: Sepehr Ariaei
 on 28 Jun 2021
            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
0 Comments
Accepted Answer
  Image Analyst
      
      
 on 26 Jun 2021
        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
  Walter Roberson
      
      
 on 27 Jun 2021
				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
  Image Analyst
      
      
 on 27 Jun 2021
				Does it always increase in size?  If not then you could replace
if FileInfo.bytes > oldsize
with
if FileInfo.bytes ~= oldsize
More Answers (1)
See Also
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!

