简体   繁体   English

Matlab - 实时绘制 3D 图形

[英]Matlab - Drawing 3D figure in real time

I'm trying to plot a 3D cylinder showing in Matlab that updates in real time, it's supposed to swap frames(images) as the value of the loop increases.我正在尝试绘制在 Matlab 中显示的实时更新的 3D 圆柱体,它应该随着循环值的增加而交换帧(图像)。 My problem however is that instead of keeping the frames within figure it opens up a new figure for every frame.然而,我的问题是,不是将帧保留在图形中,而是为每一帧打开一个新图形。

I can't use the custom colormap that I've loaded in either(which is why I'm using parula at the moment) and another problem that I have is that I can't get figures to open in the live editor, I have to use the command window.我无法使用我加载的自定义颜色图(这就是我目前使用 parula 的原因),我遇到的另一个问题是我无法在实时编辑器中打开数字,我必须使用命令窗口。 Wondering if someone might see what I'm missing here, thanks in advance!想知道是否有人可能会看到我在这里缺少的东西,在此先感谢!

My code.我的代码。

clc;
clear all;
close all;

filename = ['*The file pathway*' 'filenames.txt'];
T = readtable(filename);
tsize = size(T);
tsize (1);

filename = strcat('*The file pathway*', string(T{350,1}));
heat = double(getHeatMap(filename));

load('newColorMap.mat');

for i = 1:tsize
    filename = strcat('*The file pathway*', string(T{i,1}));
    heat = double(getHeatMap(filename));
    [X,tY] = meshgrid( linspace(1,400,size(heat,2)),linspace(0,2*pi,size(heat,1)));
    max_heat = max(heat, [], 'all');
    min_heat = min(heat, [], 'all');
    R = (((heat-min_heat)/(max_heat-min_heat))*50)+100;
    Y = cos(tY) .* R;
    Z = sin(tY) .* R;
    [nx, ny, nz] = surfnorm(X,Y,Z);
    nv = reshape([nx ny nz], size(nx,1),size(nx,2),3);
    CV = R;
    figure
    s = surf(X,Y,Z,heat,'VertexNormals',nv, 'EdgeColor','none');
    axis([0 400 -200 200 -200 200])
    colorbar
    colormap('parula')
    lighting gouraud
    camlight
    material dull
    caxis([0 80])
    drawnow
end
function heat = getCylinderHeatMap(filename)
    %Returns a struct with info about the file.
    s = dir(filename);
    %Opens a file for reading, hence the 'r'.
    fin = fopen(filename,'r');
    I=fread(fin,s.bytes,'uint8=>uint8');
    
    w = uint16(I(1))+256*uint16(I(2));
    h = uint16(I(3))+256*uint16(I(4));
    skip = s.bytes - w*h + 1;
    IN = I(skip:1:s.bytes);
    Z=single(reshape(IN,w,h));
    Z=griddedInterpolant(Z');
    y_range = linspace(1.0,single(h),360);
    x_range = linspace(1.0,single(w),512);
    heat = uint8(Z({y_range, x_range}));
end

Thanks to beaker I managed to keep the frames within the figure by keeping the figure command outside of the loop.感谢烧杯,我设法通过将 figure 命令保持在循环之外来将帧保持在图中。

I managed to solve the other two problems on my own by adding the commando set(gcf,'Visible','on') after figure to show it in a separate window from the live editor and I managed to add the custom colormap by using lowercase letter instead of using camelcase like in the filename newColorCamp.mat.我设法通过在图形之后添加突击队集(gcf,'Visible','on')以在与实时编辑器不同的窗口中显示它来自己解决另外两个问题,并且我设法通过使用添加自定义颜色图小写字母而不是像在文件名 newColorCamp.mat 中那样使用驼峰字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM