Dynamic Plot Update with for loop
30 views (last 30 days)
Show older comments
Hello, I am simulating a control process using a for loop and want to add a dynamic plot. This plot should update every few iterations to show the current Temperatures of the process. My problem is that I want to tell the plot only once which variable should be plotted and not inside the loop. Is there any possibility for that?
Currently I'm using this code at the beginning of the script:
figure
xval = zeros((t_end/delta_t),1);
x123 = 1:(tfinal/delta_t);
xval(x123) = x123;
testplot = plot(xval,Temperature_1);
testplot.XData = xval;
testplot.YData = Temperature_1;
linkdata on;
hold on
In the loop I only call
drawnow
But this code does not produce a dynamic plot but only one update after the last loop Iteration or when the loop is stopped using "Ctrl"+"C" I would like the figure to update after every few iterations of the loop but only specify the variable to be plotted once in the beginning of the code.
Thank you for your help
0 Comments
Answers (1)
Jaswanth
on 22 Oct 2024
Hi,
To update a plot dynamically during a simulation loop in MATLAB, while specifying the variable to be plotted only once at the beginning, begin by setting up the plot outside the loop.
Use a conditional statement to refresh the plot at regular intervals. This ensures that the plot displays the latest data without unnecessary updates, maintaining accuracy and performance.
Incorporate MATLAB’s “drawnow” function within the loop to refresh the plot. This command is essential for visualizing updates in real time and ensure that changes are visible after each iteration.
Please refer to the following example code demonstrating the process described above:
% Parameters
t_end = 100; % Example end time
delta_t = 1; % Example time step
n_steps = t_end / delta_t; % Total number of steps
update_interval = 5; % Update plot every 5 iterations
% Preallocate temperature data
Temperature_1 = zeros(n_steps, 1);
% Initialize figure and plot
figure;
xval = (1:n_steps)';
testplot = plot(xval, Temperature_1, 'LineWidth', 2);
xlabel('Time Step');
ylabel('Temperature');
title('Dynamic Temperature Update');
grid on;
ylim([-2, 2]); % Adjust y-limits as needed
% Simulation loop
for t = 1:n_steps
% Simulate some temperature data (replace with actual simulation)
Temperature_1(t) = sin(t * delta_t * 0.1) + randn * 0.1; % Example simulation
% Update the plot every 'update_interval' iterations
if mod(t, update_interval) == 0 || t == n_steps
% Update the YData property of the plot
set(testplot, 'YData', Temperature_1);
% Refresh the plot
drawnow;
% Optional: Pause for visibility (remove or adjust as needed)
pause(0.1);
end
end
I hope the solution provided above is helpful.
1 Comment
Walter Roberson
on 22 Oct 2024
Recording the output of plot() and later using it to set YData is the right way to go. However, it is likely that XData also needs to be updated.
Note that for some time, you do not need to
set(testplot, 'YData', Temperature_1)
and can instead use
testplot.YData = Temperature_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!