2

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

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks a lot! Really appreciate your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.