executing a plot every time interval without keeping matlab busy

4 views (last 30 days)
I have a process that updates a file every 5 minutes. The data in the file is a matrix where in the first row there are my x axis values and on each of the following rows there are values to be plotted on the y axis. every 5 minuets the matrix adds another row of y values. I want to plot the data every 5 minuets to see how it is changed over time. right now im using a while loop which works but the problem is that it keeps matlab busy. i wanted to know if there's an option to plot every 5 minutes without keeping matlab busy?
thanks
here's my code:
while a == true
hold on
cla;
blue_etching = importdata('abs during blue etching 2.dat');
plot(blue_etching(1,:),blue_etching([2 end],:));
legend('initial spectrum',[num2str(size(blue_etching,1)*5) ,' minuets'])
drawnow;
pause(300);
end

Answers (1)

Hassaan
Hassaan on 9 Jan 2024
Edited: Hassaan on 9 Jan 2024
% Run the example
updatePlotAfterIntervalExample;
Timer started. Close the figure to stop.
% main function/logic
function updatePlotAfterIntervalExample
% Dummy data initialization
x = 0:0.1:10; % X-axis values
y = sin(x); % Initial Y-axis values (1st row)
% Create a figure
hFig = figure;
hAx = axes('Parent', hFig);
% Setup a timer
t = timer;
t.Period = 2; % 2 seconds interval
t.ExecutionMode = 'fixedRate';
t.TimerFcn = @(obj, event) updatePlot(obj, event, hAx, x, y);
% Start the timer
start(t);
% Display a message about how to stop
disp('Timer started. Close the figure to stop.');
% Stop and delete timer when the figure is closed
set(hFig, 'CloseRequestFcn', @(src, evnt) stopAndDeleteTimer(src, t));
end
% Define a timer function[callback] to update and plot data
function updatePlot(obj, event, hAx, x, y)
% Simulate adding a new row of data (adding noise here for variation)
newY = sin(x) + 0.1*randn(size(x));
y(end+1, :) = newY; % Append to the matrix
% Plot
cla(hAx); % Clear axes
plot(hAx, x, y);
title(hAx, ['Data updated at: ', datestr(now)]);
drawnow;
end
function stopAndDeleteTimer(src, tmr)
stop(tmr);
delete(tmr);
delete(src);
end
  1. Run the script. It will start updating the plot every 2 seconds.
  2. To stop the process, simply close the plot window. The timer will be stopped and deleted automatically.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!