Home > Software engineering >  Matlab - Drawing 3D figure in real time
Matlab - Drawing 3D figure in real time

Time:10-25

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. 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. 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

CodePudding user response:

Thanks to beaker I managed to keep the frames within the figure by keeping the figure command outside of the loop.

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.

  • Related