Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Masking a circle out of a surface plot

Subject: Masking a circle out of a surface plot

From: Chris

Date: 1 Nov, 2010 20:44:04

Message: 1 of 4

Hello,
I am working on a project where, in the end, I will have a 3D animation of a time resolved process that will involve a changing 3D surface plot. Currently I am having a little bit of trouble with the limiting my plotting. I have used MatLab before and although I do not classify myself as a beginner, an am still not that experienced.

So basically what I am stuck on is this: I want to have a user input for a surface geometry, lets say z=(x^2)-(y^2) and I want a surface plot of that,but I only want displayed the portion of that function that within a circular region of radius r. It seems that what ever I try I can not get MatLab to display anything other then the full region of the plot axis.

How would I go about limiting the plot? Would I have to write a code that sets anything outside of that region to NaN because that is the only thing I can think of and if that is the easiest how would I code something like that? Thank you in advance.

Subject: Masking a circle out of a surface plot

From: Sean

Date: 1 Nov, 2010 21:08:03

Message: 2 of 4


> How would I go about limiting the plot? Would I have to write a code that sets anything outside of that region to NaN because that is the only thing I can think of and if that is the easiest how would I code something like that? Thank you in advance.

That's how I would do it. To determine what's outside that radius use the formula for a circle and MESHGRID; described in this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/291115

Subject: Masking a circle out of a surface plot

From: someone

Date: 1 Nov, 2010 21:52:02

Message: 3 of 4

"Chris " <cstanislawczyk@gmail.com> wrote in message <ian8qk$crn$1@fred.mathworks.com>...
> Hello,
> I am working on a project where, in the end, I will have a 3D animation of a time resolved process that will involve a changing 3D surface plot. Currently I am having a little bit of trouble with the limiting my plotting. I have used MatLab before and although I do not classify myself as a beginner, an am still not that experienced.
>
> So basically what I am stuck on is this: I want to have a user input for a surface geometry, lets say z=(x^2)-(y^2) and I want a surface plot of that,but I only want displayed the portion of that function that within a circular region of radius r. It seems that what ever I try I can not get MatLab to display anything other then the full region of the plot axis.
>
> How would I go about limiting the plot? Would I have to write a code that sets anything outside of that region to NaN because that is the only thing I can think of and if that is the easiest how would I code something like that? Thank you in advance.

% Another way is to work in polar coordinates and use meshgrid.
% In your case, something like:

theta = linspace(0,2*pi,25);
r = linspace(0,5,25);
[TH,R] = meshgrid(theta,r);
Z=((R.*cos(TH)).^2)-((R.*sin(TH)).^2); % z=(x^2)-(y^2)
[x,y,z] = pol2cart(TH,R,Z);
surf(x,y,z)

Subject: Masking a circle out of a surface plot

From: Chris

Date: 2 Nov, 2010 16:30:26

Message: 4 of 4

"someone" <someone@somewhere.net> wrote in message <iancq2$65a$1@fred.mathworks.com>...
> "Chris " <cstanislawczyk@gmail.com> wrote in message <ian8qk$crn$1@fred.mathworks.com>...
> > Hello,
> > I am working on a project where, in the end, I will have a 3D animation of a time resolved process that will involve a changing 3D surface plot. Currently I am having a little bit of trouble with the limiting my plotting. I have used MatLab before and although I do not classify myself as a beginner, an am still not that experienced.
> >
> > So basically what I am stuck on is this: I want to have a user input for a surface geometry, lets say z=(x^2)-(y^2) and I want a surface plot of that,but I only want displayed the portion of that function that within a circular region of radius r. It seems that what ever I try I can not get MatLab to display anything other then the full region of the plot axis.
> >
> > How would I go about limiting the plot? Would I have to write a code that sets anything outside of that region to NaN because that is the only thing I can think of and if that is the easiest how would I code something like that? Thank you in advance.
>
> % Another way is to work in polar coordinates and use meshgrid.
> % In your case, something like:
>
> theta = linspace(0,2*pi,25);
> r = linspace(0,5,25);
> [TH,R] = meshgrid(theta,r);
> Z=((R.*cos(TH)).^2)-((R.*sin(TH)).^2); % z=(x^2)-(y^2)
> [x,y,z] = pol2cart(TH,R,Z);
> surf(x,y,z)

Thats great. It works exactly as I would like. Thank you

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us