MATLAB: How to select a specific section of a 3D plot and show this selection in a new plot in MATLAB 7.10 (R2010a)

MATLAB

I have a 3D plot and would like to exactly select the region corresponding to a particular color in the plot. I would then like to show this selection in a new figure.

Best Answer

  • The ability to select a particular colored region and plot the selection in a new figure is available in MATLAB 7.10 (R2010a). Refer to the example code below.
    clear
    clc
    z = peaks;
    figure(1)
    surf(z)
    colorbar;
    ha(1) = gca;
    % Selecting the limits corresponding to blue color.
    upperLimit = -4;
    lowerLimit = -7;
    vi = (z < upperLimit) & (z > lowerLimit); % Matrix of logical values.
    Z = z;
    Z(~vi) = nan; % Replacing 0 with NaN in the matrix.
    figure(2)
    surf(Z); %Plotting with new values of Z.
    ha(2) = gca
    linkprop(ha, 'xlim') % LINKPROP retains the same axis limits
    linkprop(ha, 'ylim')
    linkprop(ha, 'zlim')
    linkprop(ha, 'clim')
    colorbar