# Plot data in real time

조회 수: 3 (최근 30일)
Jacque 2012년 3월 3일
답변: Sanchari 2024년 7월 17일 11:35
Hi guys,
I've put together a simulation that takes values calculated within a for loop and plots them using plot3.
Each of my values starts at 0 and the X and Y values are constrained to their initial values.
What I would like to do is plot from 0 to Z as my simulation is moving along, but using the hold function doesn't give me what I need as my Z value will go to a maximum value of 0.17 and also go to a minimum of 0, thus overlapping its previous data.
Is there a simple way of doing this?
By the way I have 4 different sets of data plotted on the same graph.
If it would help to see the code I will be mroe than happy to show it.
Thanks

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Sanchari 2024년 7월 17일 11:35
Hello Jacque,
From what I understand, the code is to dynamically update the 3D plot in MATLAB to show the simulation progression over time. One way to achieve this is to use the "plot3" function within a loop and update the plot incrementally. Use the "drawnow" function to update the figure window during each iteration of the loop.
Below is a sample code that may be useful.
% Initialize the figure
figure;
hold on;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Dynamic 3D Plot of Simulation');
% Number of data points
numPoints = 100;
% Pre-allocate arrays for the data
x1 = zeros(1, numPoints);
y1 = zeros(1, numPoints);
z1 = zeros(1, numPoints);
x2 = zeros(1, numPoints);
y2 = zeros(1, numPoints);
z2 = zeros(1, numPoints);
x3 = zeros(1, numPoints);
y3 = zeros(1, numPoints);
z3 = zeros(1, numPoints);
x4 = zeros(1, numPoints);
y4 = zeros(1, numPoints);
z4 = zeros(1, numPoints);
% Initialize plot handles
h1 = plot3(x1, y1, z1, 'r');
h2 = plot3(x2, y2, z2, 'g');
h3 = plot3(x3, y3, z3, 'b');
h4 = plot3(x4, y4, z4, 'k');
% Simulation loop
for k = 1:numPoints
% Update the data
x1(k) = rand; % Replace with your actual data calculation
y1(k) = rand; % Replace with your actual data calculation
z1(k) = 0.17 * rand; % Replace with your actual data calculation
x2(k) = rand; % Replace with your actual data calculation
y2(k) = rand; % Replace with your actual data calculation
z2(k) = 0.17 * rand; % Replace with your actual data calculation
x3(k) = rand; % Replace with your actual data calculation
y3(k) = rand; % Replace with your actual data calculation
z3(k) = 0.17 * rand; % Replace with your actual data calculation
x4(k) = rand; % Replace with your actual data calculation
y4(k) = rand; % Replace with your actual data calculation
z4(k) = 0.17 * rand; % Replace with your actual data calculation
% Update plot data
set(h1, 'XData', x1, 'YData', y1, 'ZData', z1);
set(h2, 'XData', x2, 'YData', y2, 'ZData', z2);
set(h3, 'XData', x3, 'YData', y3, 'ZData', z3);
set(h4, 'XData', x4, 'YData', y4, 'ZData', z4);
% Update the plot
drawnow;
end
Output:
In this code,
1. "Initialization" steps include creating a figure, setting up axes using "hold on", "grid on", and labeling the axes. Then it pre-allocates arrays for the data points (x1, y1, z1, etc.) to improve performance. Then plot handles "h1, h2, h3, h4) are initialized using "plot3".
2. "Simulation Loop" section starts with updating the data arrays with new values in each iteration of the loop. Please replace the "rand" function with the actual data calculations. Use the "set" function to update the "XData", "YData", and "ZData" properties of the plot handles. Call "drawnow" to update the plot in the figure window for each iteration.
Hope this help!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by