MATLAB: Give a range of Z axis instead of X and Y when graphing a surface

creating limits on z axisSymbolic Math Toolbox

Hi guys,
I am trying to graph a surface below, but instead of giving it x and y restrictions like how I have given it, can I graph in a certain range of Z values and see what X and Y values it comes up with? For example, with the surface below I want it to graph the surface between Z values of 20 and 30. Is this possible? Thanks!
x = [0:500];
y = [0:500];
[X,Y] = meshgrid(x,y);
surface1 = @(x,y) x + y;
S1 = surface1(X,Y);
surf(X,Y,S1)

Best Answer

Perhaps this is what you want:
x = 0:500;
y = 0:500;
[X,Y] = meshgrid(x,y);
surface1 = @(x,y) x + y;
subplot(2,1,1);
S1 = surface1(X,Y);
surf(X,Y,S1, 'edgecolor', 'none')
title('S1', 'FontSize', 40);
% Find out where S1 is between 20 and 30
binaryMap = S1 >= 20 & S1 <= 30;
[rows, columns] = find(binaryMap);
xLeft = min(columns)
xRight = max(columns)
yTop = min(rows)
yBottom = max(rows)
S2 = S1; % Initialize
% Set to NaN where outside range so it will be invisible.
S2(~binaryMap) = nan;
% Crop to desired region;
S2 = S2(yTop:yBottom, xLeft:xRight);
subplot(2,1,2);
surf(x(xLeft:xRight), y(yTop:yBottom), S2);
title('S2', 'FontSize', 40);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.5 0 0.3 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')