Search code examples
matlabmatlab-figure

MATLAB - how to Drawing a image and a surf in the same figure?


I have written code to draw a surf and a image in two different figures as such.enter image description here

Now I would like to place these two images in the same figure aligned horizontally. any one who could help me?

here is my code.

function cylinder = createSurfCylinder(matrix) 
%Load heat map.
load('myHeatMap.mat','myHeatMap');
Sample_Range = 255 - 0;
Temperature_Range = 450 - 50;

Multiplier = Temperature_Range/Sample_Range;
map100 = matrix.*Multiplier + 50;

Maximum_Value = 450;
Minimum_Value = 50;

%creates a image
k = imshow(map100);
%creates a colormap.
%gca returns the current axes (or standalone visualization) in the current figure.
%hence the command just works top down and affects last image displayed.
colormap(myHeatMap);
caxis([Minimum_Value Maximum_Value]);
colorbar;


%Setting up the figure%
Radius = 1.5;
Number_Of_Data_Points = 360;
theta = linspace(0,2*pi,Number_Of_Data_Points);

%The xy values according to radius and number of points%
Z_Circle = Radius*cos(theta);
Y_Circle = Radius*sin(theta);

map100 = rot90(map100);

Height = 512;
Z_Circle = repmat(Z_Circle,Height,1);
Y_Circle = repmat(Y_Circle,Height,1);
X_Length = (1:512)';
X_Length = repmat(X_Length,1,Number_Of_Data_Points);

figure('Position', [10 10 500 500])

%surf(X_Circle,Y_Circle,Z_Height,'Cdata',map100); vertical
cyl = surf(X_Length,Y_Circle,Z_Circle,'Cdata',map100); 
title("3D Heatmap Plot");
zlabel("Z-Position");
ylabel("Y-Position");
xlabel("Length(Cm)");
%Reverse Y axis.
set(gca,'Ydir','reverse')
colormap(myHeatMap);
colorbar;
shading interp

max(map100,[],'all')
caxis([Minimum_Value Maximum_Value]);
cylinder = cyl;
end 

I have tried to use subplot but it just does not turn out right iv been stuck on this one thing for 4h now and could really use some help.

Thank you.


Solution

  • Using the subplot() function will allow you to create a figure containing both plots/images. The subplot function takes in 3 arguments which essentially creates an array/grid where you can insert plots/images.

    Pseudo Function Call: subplot(Rows,Columns,Position);

    The Position parameter follows the same indexing as array indexing. You may also use multiple grid slots. To use grid slots 1 and 2 for a plot set the Position argument to 1:2 or if you'd like to use grid slots 1 to 3 for a plot set the Position argument to 1:3. The Rows argument sets the number of rows within the grid and the Columns argument sets the number of columns in this example.

    Example Diagram: 2 by 3 Grid (6 positions)

    Subplot Diagram

    Line Change 1:

    figure('Position', [10 10 800 500])
    

    Line Change 2:

    subplot(1,3,1:2); Surface = surf(Z_Points,Y_Points,X_Points,'Cdata',map100);
    

    Line Change 3:

    subplot(1,3,3); imshow((rot90(map100))); 
    

    Ran using MATLAB R2019b