Slow 3D video rendering & underutilized system usage
9 次查看(过去 30 天)
显示 更早的评论
I'm new to matlab and trying to create a basic 3D plot movie in matlab only to find my system severly underutelized and the plotting code running extremely slow. Everything is preallocated and precaluclated and it's only the loop where the frames are captured that is very slow. It captures 1200 frames at 80 fps so it's a 15 second animtion. This takes around 6 minutes to complete where when i checked my CPU & GPU usage on task manger they dont go over ~20% and ram usage also peaks around ~60%. I'm looking for some advice on what would be done here to improve this even slightly.
I also tried to have
set(f, 'Visible', 'on');
set to off only for no changes to occur & parfor seems to just go slower.
Thanks!
The code:
%E_t is a 200*200*1200 double
f = figure;
set(f, 'Visible', 'on');
set(f, 'Renderer', 'opengl');
figureWidth = 1800; % Adjust as needed
figureHeight = 900; % Adjust as needed
set(gcf, 'Position', [50, 50, figureWidth, figureHeight]);
myFrames = cell(numSamplesTime,1);
myFrames(:) = {zeros(figureHeight, figureWidth, 3, 'uint8')};
colorMap = cell(numSamplesTime,1);
colorMap(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
myMovie = struct('cdata', myFrames, 'colormap', colorMap);
tic
maxValue = max(E_t(:));
for tIndex= 1 : numSamplesTime - 1
subplot(1,2,1);
imagesc(x,y,E_t(:,:,tIndex)'); colorbar; clim([0, maxValue]); colormap("jet");
title("|E(x,y,t)| Heatmap")
xlabel('X');
ylabel('Y');
axis square;
subplot(1,2,2);
mesh(x,y,E_t(:,:,tIndex)'); colorbar; clim([0, maxValue]); colormap("jet");
title("|E(x,y,t)| 3D-plot")
xlabel('X');
ylabel('Y');
zlabel('|E(t)|');
axis([0 a 0 a 0 maxValue]);
axis vis3d;
thisFrame = getframe(f);
myMovie(tIndex) = thisFrame;
end
close(f);
toc
%output: Elapsed time is 435.840855 seconds.
A frame of the animation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582051/image.png)
Task manger with simualtion running:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582056/image.png)
Profiling results (which i dont understand but am sure will help somehow?)![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582061/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1582061/image.png)
0 个评论
采纳的回答
Walter Roberson
2024-1-3
Instead of setting everything up every iteration, set up once before-hand and update objects inside iterations.
tIndex = 1;
subplot(1,2,1);
img121 = imagesc(x,y,E_t(:,:,tIndex)');
colorbar;
clim([0, maxValue]);
colormap("jet");
title("|E(x,y,t)| Heatmap")
xlabel('X');
ylabel('Y');
axis square;
subplot(1,2,2);
mesh122 = mesh(x,y,E_t(:,:,tIndex)');
colorbar;
clim([0, maxValue]);
colormap("jet");
title("|E(x,y,t)| 3D-plot")
xlabel('X');
ylabel('Y');
zlabel('|E(t)|');
axis([0 a 0 a 0 maxValue]);
axis vis3d;
for tIndex= 1 : numSamplesTime - 1
thisdata = E_t(:,:,tIndex)';
img121.CData = thisdata;
mesh122.CData = thisdata;
pause(0.1)
thisFrame = getframe(f);
myMovie(tIndex) = thisFrame;
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!