MATLAB: In a 3d graph. Can we put limits on z axis w.r.t. x and y axis

graphicslimitmathematical calculationplot

if true
% code
function [Kraftstoffverbrauch, Drehzahl, Drehmoment]=Kasus2_1(Drehzahl, Drehmoment)
% Modify the dimensions
nx = length(Drehzahl) ;
ny = length(Drehmoment) ;
if nx > ny
Drehmoment1 = linspace(min(Drehmoment),max(Drehmoment),nx ) ;
elseif nx<ny
Drehzahl1 = linspace(min(Drehzahl),max(Drehzahl),ny) ;
end
[num,txt,raw] = xlsread('Mappe2.xlsx') ;
Dz = num(:,1) ; Dz(isnan(Dz))= [ ];
Dm = num(:,2) ;Dm(isnan(Dm))= [ ];
Kv = num(:,3) ;Kv(isnan(Kv))= [ ];
F = scatteredInterpolant([Dz Dm],Kv);
for i= 1:1:nx
for j= 1:1:ny
Kraftstoffverbrauch (i,j) = F(Drehzahl(i),Drehmoment(j));
end
end
surf(Drehzahl,Drehmoment,Kraftstoffverbrauch'),xlabel ('Drehzahl(1/min)');ylabel ('Drehmoment(N.m)'); zlabel ('Kraftstoffverbrauch(kWh/100km)');
title ('Drehmoment vs Drehzahl Diagram');
end
The display of he expected limits can be sssn in the picture

Best Answer

I made your code a bit faster and fixed your problem.
%%Load data
[num,txt,raw] = xlsread('Mappe2.xlsx') ;
x = num(:,1);
y = num(:,2);
z = num(:,3);
x(isnan(x)) = [];
y(isnan(y)) = [];
z(isnan(z)) = [];
%%Interpolate
F = scatteredInterpolant([x y],z);
[X,Y] = meshgrid(1000:5500,0:250);
Z = F(X,Y);
%%Define region where you want to keep data
xp = 1000:250:5500;
yp =[160,180,200,220,220,218,216,215,219,220,220,220,220,220,220,210,200,195,175];
ax = gca;
xp = [xp max(xp) min(xp) min(xp)];
yp = [yp min(Y(:)) min(Y(:)) yp(1)];
%%Find points inside of this region and remove others
in = inpolygon(X(:),Y(:),xp,yp);
Z(~in) = NaN;
%%Plot
figure;hold on
surf(X,Y,Z,'edgecolor','none')