Matlab - 实时绘制3D图形

Matlab - Drawing 3D figure in real time

我正在尝试绘制一个在 Matlab 中实时更新的 3D 圆柱体,它应该随着循环值的增加而交换帧(图像)。 然而,我的问题是,它没有将帧保留在图形中,而是为每一帧打开一个新图形。

我也无法使用我加载的自定义颜色图(这就是我现在使用 parula 的原因),我遇到的另一个问题是我无法在其中打开图形现场编辑器,我必须使用命令 window。想知道是否有人能看到我在这里遗漏的内容,在此先感谢!

我的代码。

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

多亏了烧杯,我通过将 figure 命令保持在循环之外,设法将帧保持在图形中。

我设法自己解决了另外两个问题,方法是在图后面添加突击队集(gcf,'Visible','on')以在单独的 window 中显示它实时编辑器和我设法通过使用小写字母而不是像文件名 newColorCamp.mat.

中那样使用驼峰字母来添加自定义颜色图