- "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".
- "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.
Plot data in real time
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
Sanchari
2024 年 7 月 17 日
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,
Hope this help!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!