Kristian
Kristian

Reputation: 1239

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 :)

Upvotes: 2

Views: 3784

Answers (2)

tmpearce
tmpearce

Reputation: 12683

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.

Upvotes: 6

Peter
Peter

Reputation: 14927

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.

Upvotes: 1

Related Questions