简体   繁体   中英

MATLAB plot part of surface

Say I have the following script:

u = -5:.2:5;
[X,Y] = meshgrid(u, u);
Z = cos(X).*cos(Y).*exp(-sqrt(X.^2 + Y.^2)/4);
surf(X,Y,Z);

Is there anyway that I can make MatLab plot only parts of the surface? Say, for instance, I just want to plot a single point, or a single grid, what can I do? I thought perhaps to plot a single point I could use:

surf(X(1,1), Y(1,1), Z(1,1))

But then I get the error message:

??? Error using ==> surf at 78
Data dimensions must agree.

I would really appreciate some input/help here. Thanks in advance :)

When I try what you tried, I get the following:

surf(X(1,1),Y(1,1),Z(1,1))
Error using surf (line 75) Z must be a matrix, not a scalar or vector.

So the problem is that you can't do just a point or line using surf , you'd have to use a different function. But you can select subregions

>> ii=1:5;
>> jj=1:20;
>> surf(X(ii,jj),Y(ii,jj),Z(ii,jj))

Another way to do it is to use NaNs as a mask.

>> mask = ones(size(X));
>> mask(1:20, 20:end) = nan;
>> surf(X.*mask, Y.*mask, Z.*mask)

This will make the parts where NANs are present not be displayed.

To display only a single point, you might like the function scatter3, designed for point clouds.

scatter3(X(1,1), Y(1,1), Z(1,1))

Of course, it also works on vectors of X,Y,Z points. You can also directly specify point size and color for each point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM