Got Questions? Get Answers.
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:
using mesh to make 3D plot

Subject: using mesh to make 3D plot

From: Windell

Date: 8 Sep, 2009 10:00:23

Message: 1 of 26

Hi,

I'm attempting to plot a 3D radiation pattern of an antenna that I tested. I stored all the power levels at different azimuths and elevations in a txt file. I convert my results to rectangular coordinates using the sph2cart function and I attempt to mesh the data to create a 3D plot. In doing so I received an error: "Warning: Z must not be a scalar or vector, not rendering surface." I'm new to plotting in 3D in matlab so I'm not sure what I should do from here. I would greatly appreciate your help!

Thanks for your time!
Windell

%load & store the signal angle of attack values.
load monopole3D.txt
theta = monopole3D(:,1);
phi=monopole3D(:,2);
r=monopole3D(:,3);
%convert to radians.
theta=(pi/180)*theta;
phi=(pi/180)*phi;
%make the radius not negative.
r=r+40;
%convert spherical coordinates to rectangular coordinates.
[X Y Z]=sph2cart(theta,phi,r);
figure;
%plot in 3D
mesh(X,Y,Z)

Subject: using mesh to make 3D plot

From: Wayne King

Date: 8 Sep, 2009 12:38:02

Message: 2 of 26

"Windell " <windelljones@gmail.com> wrote in message <h859vn$802$1@fred.mathworks.com>...
> Hi,
>
> I'm attempting to plot a 3D radiation pattern of an antenna that I tested. I stored all the power levels at different azimuths and elevations in a txt file. I convert my results to rectangular coordinates using the sph2cart function and I attempt to mesh the data to create a 3D plot. In doing so I received an error: "Warning: Z must not be a scalar or vector, not rendering surface." I'm new to plotting in 3D in matlab so I'm not sure what I should do from here. I would greatly appreciate your help!
>
> Thanks for your time!
> Windell
>
> %load & store the signal angle of attack values.
> load monopole3D.txt
> theta = monopole3D(:,1);
> phi=monopole3D(:,2);
> r=monopole3D(:,3);
> %convert to radians.
> theta=(pi/180)*theta;
> phi=(pi/180)*phi;
> %make the radius not negative.
> r=r+40;
> %convert spherical coordinates to rectangular coordinates.
> [X Y Z]=sph2cart(theta,phi,r);
> figure;
> %plot in 3D
> mesh(X,Y,Z)
Hi Windell, mesh() is expecting that at least your Z argument is a matrix. I would suggest using meshgrid().

>>doc meshgrid

to convert your X,Y,Z outputs to matrices amenable to calling mesh().

Hope that helps,
wayne

Subject: using mesh to make 3D plot

From: Windell

Date: 9 Sep, 2009 00:27:01

Message: 3 of 26

Thanks for your reply Wayne! Just tried what you suggested and I'm still receiving an error. Here is my code.

%load & store the signal angle of attack values.
load monopole3D.txt
theta = monopole3D(:,1);
phi=monopole3D(:,2);
r=monopole3D(:,3);

%convert to radians.
theta=(pi/180)*theta;
phi=(pi/180)*phi;

%make the radius not negative.
r=r+40;

%convert spherical coordinates to rectangular coordinates.
[x,y,z]=sph2cart(theta,phi,r);
[X,Y,Z]=meshgrid(x,y,z);

%plot in 3D
mesh(X,Y,Z)

The error I'm getting is the following: "Error in ==> meshgrid at 62
    zz = zz(ones(ny,1),ones(nx,1),:); Error in ==> v2_plot at 123
[X,Y,Z]=meshgrid(x,y,z);"

Much Appreciated
Windell

"Wayne King" <wmkingty@gmail.com> wrote in message <h85j7a$dkh$1@fred.mathworks.com>...
> "Windell " <windelljones@gmail.com> wrote in message <h859vn$802$1@fred.mathworks.com>...
> > Hi,
> >
> > I'm attempting to plot a 3D radiation pattern of an antenna that I tested. I stored all the power levels at different azimuths and elevations in a txt file. I convert my results to rectangular coordinates using the sph2cart function and I attempt to mesh the data to create a 3D plot. In doing so I received an error: "Warning: Z must not be a scalar or vector, not rendering surface." I'm new to plotting in 3D in matlab so I'm not sure what I should do from here. I would greatly appreciate your help!
> >
> > Thanks for your time!
> > Windell
> >
> > %load & store the signal angle of attack values.
> > load monopole3D.txt
> > theta = monopole3D(:,1);
> > phi=monopole3D(:,2);
> > r=monopole3D(:,3);
> > %convert to radians.
> > theta=(pi/180)*theta;
> > phi=(pi/180)*phi;
> > %make the radius not negative.
> > r=r+40;
> > %convert spherical coordinates to rectangular coordinates.
> > [X Y Z]=sph2cart(theta,phi,r);
> > figure;
> > %plot in 3D
> > mesh(X,Y,Z)
> Hi Windell, mesh() is expecting that at least your Z argument is a matrix. I would suggest using meshgrid().
>
> >>doc meshgrid
>
> to convert your X,Y,Z outputs to matrices amenable to calling mesh().
>
> Hope that helps,
> wayne

Subject: using mesh to make 3D plot

From: Christopher Rouxel

Date: 12 Aug, 2010 15:48:24

Message: 4 of 26

This GUI might help you:
http://www.mathworks.com/matlabcentral/fileexchange/28177-3d-performance-map-gui

Subject: using mesh to make 3D plot

From: christian

Date: 24 Apr, 2013 10:29:10

Message: 5 of 26

"Windell " <windelljones@gmail.com> wrote in message <h86sol$1h9$1@fred.mathworks.com>...
> Thanks for your reply Wayne! Just tried what you suggested and I'm still receiving an error. Here is my code.
>
> %load & store the signal angle of attack values.
> load monopole3D.txt
> theta = monopole3D(:,1);
> phi=monopole3D(:,2);
> r=monopole3D(:,3);
>
> %convert to radians.
> theta=(pi/180)*theta;
> phi=(pi/180)*phi;
>
> %make the radius not negative.
> r=r+40;
>
> %convert spherical coordinates to rectangular coordinates.
> [x,y,z]=sph2cart(theta,phi,r);
> [X,Y,Z]=meshgrid(x,y,z);
>
> %plot in 3D
> mesh(X,Y,Z)
>
> The error I'm getting is the following: "Error in ==> meshgrid at 62
> zz = zz(ones(ny,1),ones(nx,1),:); Error in ==> v2_plot at 123
> [X,Y,Z]=meshgrid(x,y,z);"
>
> Much Appreciated
> Windell
>
> "Wayne King" <wmkingty@gmail.com> wrote in message <h85j7a$dkh$1@fred.mathworks.com>...
> > "Windell " <windelljones@gmail.com> wrote in message <h859vn$802$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I'm attempting to plot a 3D radiation pattern of an antenna that I tested. I stored all the power levels at different azimuths and elevations in a txt file. I convert my results to rectangular coordinates using the sph2cart function and I attempt to mesh the data to create a 3D plot. In doing so I received an error: "Warning: Z must not be a scalar or vector, not rendering surface." I'm new to plotting in 3D in matlab so I'm not sure what I should do from here. I would greatly appreciate your help!
> > >
> > > Thanks for your time!
> > > Windell
> > >
> > > %load & store the signal angle of attack values.
> > > load monopole3D.txt
> > > theta = monopole3D(:,1);
> > > phi=monopole3D(:,2);
> > > r=monopole3D(:,3);
> > > %convert to radians.
> > > theta=(pi/180)*theta;
> > > phi=(pi/180)*phi;
> > > %make the radius not negative.
> > > r=r+40;
> > > %convert spherical coordinates to rectangular coordinates.
> > > [X Y Z]=sph2cart(theta,phi,r);
> > > figure;
> > > %plot in 3D
> > > mesh(X,Y,Z)
> > Hi Windell, mesh() is expecting that at least your Z argument is a matrix. I would suggest using meshgrid().
> >
> > >>doc meshgrid
> >
> > to convert your X,Y,Z outputs to matrices amenable to calling mesh().
> >
> > Hope that helps,
> > wayne


Hi windell
i have the same issue. have you find any solution?
best regards
Christian

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 25 Apr, 2013 18:58:08

Message: 6 of 26

Hi christian,
Explain the problom to me with code, May be i can help you....

Subject: using mesh to make 3D plot

From: christian

Date: 28 Apr, 2013 01:49:09

Message: 7 of 26

"Yehonatan " <math8yehonatan@gmail.com> wrote in message <klbuc0$4bf$1@newscl01ah.mathworks.com>...
> Hi christian,
> Explain the problom to me with code, May be i can help you....


Hi yehonatan
thanks for your interest
this is my code
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
theta=[0 pi/2 pi/2 0.554 5.729 5.729 0.554 0.554 5.729 5.729 0.554 1.206 5.077 5.077 1.206];
phi=[0 0 pi/2 0.314 0.314 -0.314 -0.314 0.942 0.942 -0.942 -0.942 0.524 0.524 -0.524 -0.524];
r=[0.01 0.01 0.01 0.0334 0.0334 0.0334 0.0334 0.0273 0.0273 0.0273 0.0273 0.0351 0.0351 0.0351 0.0351];
[theta,phi,r]=meshgrid(theta,phi,r);
[x,y,z]=sph2cart(theta,phi,r);
surf(x,y,z)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
may be the data are not good. But i just want to now how to plot a 3D surface with a data list like this
best regards

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 28 Apr, 2013 06:17:11

Message: 8 of 26

OK Christian,
From what i understand you don't have any experience with matlab. I see here you have a list of data, i guess you created some where else. I have to know if this data can be created by a math function. In other words, can you create your data list by this kind of code:
x = 1:50;
y = 1:50;
z = sin(x) + cos(y);

the reason I'm asking you is because there are no fast learning solution for this problem. It's not a big problem but its not a small one either. I want you to tell me exactly what do you want to do and how much learning you want to invest.

Need To Know:
What are your x,y,z
what kind of 3d plot: surface, contour, curve
can you create the data list by a function like the above code
how much you want to invest in matlab experience

Best Regards

Subject: using mesh to make 3D plot

From: christian

Date: 28 Apr, 2013 14:13:11

Message: 9 of 26

hi yehonatan
Indeed i have any experience with matlab plotting. And i am willing to learn more about it.
the data list especially for the "r" vector are obtained by a numerical simulation.
the "r'' vector is associated to the [theta,phi] direction
and [x,y,z] is the representation in cartesian coordinates of the curve i want to plot to define the variation of "r" in the all space.

thanks for your help
best regards

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 28 Apr, 2013 15:23:10

Message: 10 of 26

OK Christian, Lets start...
The 3d plot field in matlab is a big one, therefore we don't intend to learn all of it, just the one we need. From what i can tell you want a 3d curve plot. We do not need to use meshgrid for that matter nor we need to learn how. Meshgrid is used for a surface or mesh plots, not a curve one. In light of this, what we need is simply three vectors of data for each x,y and z dimensions. Just create three vectors like theta,phi and r, then use plot3 in order to plot them.
If that does not suit you, next time specify the type of plot you want and explain to me with minimum of detail as you can how to create the data you want to plot without using the 3d plot tools like meshgrid you used earlier.

Subject: using mesh to make 3D plot

From: christian

Date: 28 Apr, 2013 16:49:09

Message: 11 of 26

hi yehonatan
https://www.dropbox.com/s/t62do1b3zkdk0bf/3d%20distribution.png
the link above is the picture of the result of plotting i want to carry out.
the threes vectors list I send before are the only data in my possesion.
theta and phi represent the spherical coordinates of 15 points for 15 directions take on a unit sphere.
and "r" represent a value linked to those points and obtained by a numerical simulation in micromechanics modelling. there is no way to create by another way other point without 3d plot.
thanks for help
best regards

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 28 Apr, 2013 18:32:09

Message: 12 of 26

This is a biggy....
Christian, you are aiming for a shape surface. It's not impossible but it is not easy either.
In order to create a shape like surface in matlab you have to understand the basics of surface plot function. Before we continue I must inform you that the task ahead might take some time to achieve. For starters, let’s try to understand how surf function works. If we will give the surf function three matrices for x,y and z this is what it will do:

Here we have three matrices I created for the surf function (no need to understand how I did it and why for now):

x =

     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5


y =

     1 1 1 1 1
     2 2 2 2 2
     3 3 3 3 3
     4 4 4 4 4
     5 5 5 5 5


z =

    2.0000 5.0000 10.0000 17.0000 26.0000
    1.5000 4.5000 9.5000 16.5000 25.5000
    1.3333 4.3333 9.3333 16.3333 25.3333
    1.2500 4.2500 9.2500 16.2500 25.2500
    1.2000 4.2000 9.2000 16.2000 25.2000


the surf function will look at all the combinations of 4 data points that are close together. For instance: [(1,1),(1,2),(2,1),(2,2)] ,[3,3),(3,4),(4,3),(4,4)] and
[(1,2),(1,3),(2,2),(2,3)]. Each parentheses represent a correspond cell in all the matrices, in other words it represent a data point that consist of three dimensions. Each bracket is an example of four data points that the surf function uses in the creation of the 3d plot. The surf function takes all of the combinations of four data points like I showed above and create out of each one a 3d surface square. So, in order to use surf function properly we need not only to give the surf function data points but also we need to organize the data points in the matrices we intend to give the surf function in such a way that all of the four points combinations in the matrices will be a 3d square we want to be displayed in the result 3d plot.

Here is a video I created that shows the inner working of the surf function.:
http://www.youtube.com/watch?v=poUTVYqu_Z4&feature=youtu.be

Christian, this is the first step. When we understand it we will move to the next. Now,
If you don’t understand something here ask, if it’s too much for you try another program but this is how it is done in matlab as far as I know.
I’ll be gladly continue teaching you, it’s your call.

Best regards.

Subject: using mesh to make 3D plot

From: christian

Date: 28 Apr, 2013 21:55:09

Message: 13 of 26

hi yehonathan
gratitude for your time.
 in the bracket why four data points instead of three?

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 28 Apr, 2013 23:17:09

Message: 14 of 26

Because the surf function creates 3d squares and not 3d triangles. Every surface part the surf function creates is a 3d square, therefore it need 4 data points. If you saw the youtube video, you can see that in the bottom of the sphere shape it looks like the surf function creates triangles but in fact its a square that have a twin data point.
If you ask why it is done that way i don't know, this is how the surf function programmers implemented the surf function.

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 28 Apr, 2013 23:42:12

Message: 15 of 26

For further understanding, In the YouTube video I created the sphere x,y and z matrices using the matlab sphere function. In the first section I plotted the data point that I created using the plot3 function. In the second section I took every four combination of data points and told the surf function to plot. With that said, you can see how all of the combinations combined create the result sphere surface.

Subject: using mesh to make 3D plot

From: christian

Date: 28 Apr, 2013 23:54:10

Message: 16 of 26

about combination how are you define the order?

Subject: using mesh to make 3D plot

From: christian

Date: 29 Apr, 2013 00:03:09

Message: 17 of 26

and also in the example you posted [(1,1),(1,2).......] represent the lines and rows of all the matrix (x,y,z) or just the matrix Z ?

Subject: using mesh to make 3D plot

From: christian

Date: 29 Apr, 2013 00:52:09

Message: 18 of 26

hi yehonatan
i finally understand about the four datas and the different couple (i,j). i still confuse about the order of all the combination

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 29 Apr, 2013 01:30:10

Message: 19 of 26

Ok...
For the second question, Yes... It defines the rows and columns of all the three matrices.
(1,2) is the cell in x,y and z matrices in the location of row 1 and column 2.

For the first question, it doesn't matter how the surf function goes over the combinations.
Think of it like this, the surf function does not create one surface. Instead it creates a lot of individual’s surfaces. What are these individuals’ surfaces is determined by every combination of four data points that are close together. So, you can go over the combinations in any way you like as long as you use each one of them.

If you look at the YouTube video you can see how I was able to play with the surf function. I went over the individuals surfaces in tow different ways. As long as you organize the matrices so that each four data points represents an individual surface you wish to plot you'll be able to plot anything you want.

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 29 Apr, 2013 02:32:09

Message: 20 of 26

So,
If you can figure out what are yours individuals surfaces you can use surf to plot them in tow general ways:
1) If you can combine all of the individual’s surfaces in to one x,y and z matrices then do it.
2) If you can't combine all of the individual’s surfaces you can plot them one at a time like I did in the YouTube video.

remember that each individual surface has four data point, each data point consist of three data point for each of the x,y and z dimensions.

The act of creating a single matrix for each of the three dimensions is not an easy one when dealing with shapes like surface you showed me earlier. The problem lies in the fact that each individual surface uses the same data points as the individual’s surfaces next to it. So, it’s quite a difficult organizing task.

Subject: using mesh to make 3D plot

From: christian

Date: 29 Apr, 2013 13:04:07

Message: 21 of 26

Hi yehonatan
What is the surf syntax to plot each individual square?

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 29 Apr, 2013 13:49:09

Message: 22 of 26

Let's see now an example...
If I want to plot this 3d squares:

Square 1:

x1 =

     1 2
     1 2
           


y1 =

     1 1
     2 2
              

z1 =

     1 4
     1 4
     

Square 2:

x2 =

          2 3
          2 3
              


y2 =

          1 1
          2 2
              


z2 =

         4 9
         4 9
         

Square 3:

x3 =

               
     1 2
     1 2


y3 =

               
     2 2
     3 3


z3 =

              
     1 4
     1 4

Square 4:

x4 =

               
          2 3
          2 3


y4 =

               
          2 2
          3 3


z4 =

            
         4 9
         4 9
     
  
I can plot all of these individuals’ squares like this:

figure(1) % Open a figure window

hold on % everything I plot will be saved and will not be erased by new plot command
        
surf(x1,y1,z1);
surf(x2,y2,z2);
surf(x3,y3,z3);
surf(x4,y4,z4);

Another way to plot all of this individuals squares is by creating one matrix for x,y and z that will contain all of the squares:

x5 =

     1 2 3
     1 2 3
     1 2 3


y5 =

     1 1 1
     2 2 2
     3 3 3


z5 =

     1 4 9
     1 4 9
     1 4 9

Go over all the four points combinations and see for yourself how we have in this matrices all of the individuals squares we plotted earlier. This is plotted in the same way as before. Let’s create another figure and compare the two ways:

figure(2) % open another figure window

surf(x5,y5,z5);

Use the rotate button the check the tow surfaces and see for yourself that there are alike.

Subject: using mesh to make 3D plot

From: christian

Date: 29 Apr, 2013 14:24:10

Message: 23 of 26

thanks i understand clearly this part
now for my issue (plot a shape surface in the same way like the picture i send in the link) can you check this code
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
theta=[0 pi/2 pi/2 0.554 5.729 5.729 0.554 0.554 5.729 5.729 0.554 1.206 5.077 5.077 1.206];
phi=[0 0 pi/2 0.314 0.314 -0.314 -0.314 0.942 0.942 -0.942 -0.942 0.524 0.524 -0.524 -0.524];
t=[0.01 0.01 0.01 0.0334 0.0334 0.0334 0.0334 0.0273 0.0273 0.0273 0.0273 0.0351 0.0351 0.0351 0.0351];
[theta,phi]=meshgrid(theta,phi);
r=diag(t);
[x,y,z]=sph2cart(theta,phi,r)
surf(x,y,z)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
it seems like i have one part of the surface but may be i need to create by interpolation more point to have a refined mesh. what's your point?

Subject: using mesh to make 3D plot

From: Yehonatan

Date: 29 Apr, 2013 15:19:09

Message: 24 of 26

Ummmmm......

Before we continue you must be sure you understand everything I said, if not ask again.

Now, its better we talk on email because I need to show you some pictures to explain the new stuff. For now, I used the code you sent me to show me the data points your code generated. It seems you don't have enough data to create the plot you showed me, furthermore, I looked at the theta,phi,t data points and then looked at the x,y,z data points. The x,y,z data points are useless (they can’t create any useful surface), for the theta,phi,t data points I choose which is x,y and z arbitrary and then I plotted the data points. It seemed that the theta, phi, t data points are capable of creating a useful surface but there is not enough data to do so.

I don’t understand the background of you data points. It’s not a good idea to explain me the background either. Instead try to ask me matlab regarding questions.
You need to aim towards this: Don’t think of plotting a function, instead think of plotting data points. If, for example you didn’t have a computer what would you do? Youll’d be creating the data points, then you would draw them on paper and you would connect the data points so that they will create the shape you want. Therefore, try to give me data points with x,y and z coordinates.

When we will have the data points then I will help with the organizing task. I can’t do the math for you, I can help you implement your math in matlab.
Best Regards….

Subject: using mesh to make 3D plot

From: christian

Date: 29 Apr, 2013 17:54:10

Message: 25 of 26

hi yehonatan
i send you a mail for asking about pictures. i hope you receive it

Subject: using mesh to make 3D plot

From: Steven_Lord

Date: 29 Apr, 2013 17:57:49

Message: 26 of 26



"Yehonatan " <math8yehonatan@gmail.com> wrote in message
news:klkall$lks$1@newscl01ah.mathworks.com...
> Because the surf function creates 3d squares and not 3d triangles. Every
> surface part the surf function creates is a 3d square, therefore it need 4
> data points. If you saw the youtube video, you can see that in the bottom
> of the sphere shape it looks like the surf function creates triangles but
> in fact its a square that have a twin data point.
> If you ask why it is done that way i don't know, this is how the surf
> function programmers implemented the surf function.

FYI if you want to create a surface plot whose faces are triangles, look at
the TRISURF function.

http://www.mathworks.com/help/matlab/ref/trisurf.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for 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