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:
Plotting 3D graph using plot3

Subject: Plotting 3D graph using plot3

From: Brandon

Date: 21 Mar, 2013 17:47:18

Message: 1 of 4

I have the following code and I am trying to plot a 3D graph using the x,y,z coodinates although when I run the function the graph shows one line. New matlab user. Can someone help?!?!?

%mag = rand(1,682)
%phi = 30
function [ x, y, z ] = LidarCoord3D( magnitude, phi )

   for s = 44 : 725;
            magnitude(s-43);
            m = magnitude;
            
            theta = ((s-44)*.3516) + (-30); %round
            
            
            x = m*cosd(theta);
            y = m*sind(theta)*cosd(phi);
            z = m*sind(theta)*sind(phi);
            
          plot3(x,y,z);
   end
end

Subject: Plotting 3D graph using plot3

From: anyone

Date: 21 Mar, 2013 18:46:21

Message: 2 of 4

On Thu, 21 Mar 2013 17:47:18 +0000, Brandon wrote:

> I have the following code and I am trying to plot a 3D graph using the
> x,y,z coodinates although when I run the function the graph shows one
> line. New matlab user. Can someone help?!?!?
>
> %mag = rand(1,682)
> %phi = 30
> function [ x, y, z ] = LidarCoord3D( magnitude, phi )
>
> for s = 44 : 725;
> magnitude(s-43);
> m = magnitude;
>
> theta = ((s-44)*.3516) + (-30); %round
>
>
> x = m*cosd(theta);
> y = m*sind(theta)*cosd(phi);
> z = m*sind(theta)*sind(phi);
>
> plot3(x,y,z);
> end
> end

aside from the purpose of the function/loop -- done that way -- you over-write x, y, and z on each pass, then attempt
a 3-D plot of one value for each. it looks as if you're trying to plot incrementally. instead, think about saving derived
values of x, y, and z as vectors; plot everything as a single last step, outside the loop.

Subject: Plotting 3D graph using plot3

From: Brandon

Date: 21 Mar, 2013 18:56:06

Message: 3 of 4

anyone <jaup@h/WSWLG{nR1³qHe> wrote in message <514b557d$0$7576$c3e8da3$12bcf670@news.astraweb.com>...
> On Thu, 21 Mar 2013 17:47:18 +0000, Brandon wrote:
>
> > I have the following code and I am trying to plot a 3D graph using the
> > x,y,z coodinates although when I run the function the graph shows one
> > line. New matlab user. Can someone help?!?!?
> >
> > %mag = rand(1,682)
> > %phi = 30
> > function [ x, y, z ] = LidarCoord3D( magnitude, phi )
> >
> > for s = 44 : 725;
> > magnitude(s-43);
> > m = magnitude;
> >
> > theta = ((s-44)*.3516) + (-30); %round
> >
> >
> > x = m*cosd(theta);
> > y = m*sind(theta)*cosd(phi);
> > z = m*sind(theta)*sind(phi);
> >
> > plot3(x,y,z);
> > end
> > end
>
> aside from the purpose of the function/loop -- done that way -- you over-write x, y, and z on each pass, then attempt
> a 3-D plot of one value for each. it looks as if you're trying to plot incrementally. instead, think about saving derived
> values of x, y, and z as vectors; plot everything as a single last step, outside the loop.


How can I save the values so when i plot outside the loop all the values are there?
If I just plot outside the loop, the same graph results.
Thanks in advance.

Subject: Plotting 3D graph using plot3

From: Steven_Lord

Date: 21 Mar, 2013 21:01:01

Message: 4 of 4



"Brandon " <bmfilo@verizon.net> wrote in message
news:kifl46$224$1@newscl01ah.mathworks.com...
> anyone <jaup@h/WSWLG{nR1qHe> wrote in message
> <514b557d$0$7576$c3e8da3$12bcf670@news.astraweb.com>...
>> On Thu, 21 Mar 2013 17:47:18 +0000, Brandon wrote:
>>
>> > I have the following code and I am trying to plot a 3D graph using the
>> > x,y,z coodinates although when I run the function the graph shows one
>> > line. New matlab user. Can someone help?!?!?
>> >
>> > %mag = rand(1,682)
>> > %phi = 30 function [ x, y, z ] = LidarCoord3D( magnitude, phi )
>> >
>> > for s = 44 : 725;
>> > magnitude(s-43);
>> > m = magnitude;
>> > theta = ((s-44)*.3516) + (-30); %round
>> > x = m*cosd(theta);
>> > y = m*sind(theta)*cosd(phi);
>> > z = m*sind(theta)*sind(phi);
>> > plot3(x,y,z);
>> > end
>> > end
>>
>> aside from the purpose of the function/loop -- done that way -- you
>> over-write x, y, and z on each pass, then attempt a 3-D plot of one value
>> for each. it looks as if you're trying to plot incrementally. instead,
>> think about saving derived values of x, y, and z as vectors; plot
>> everything as a single last step, outside the loop.
>
>
> How can I save the values so when i plot outside the loop all the values
> are there?

You will need to create an array. A simple example:

for k = 1:10
    values(k) = k+1;
end

This creates a 1-by-10 vector and fill it in with values. There are more
efficient ways to do this, though. One such way is to preallocate then fill
it in, rather than forcing the vector to grow by adding a new element to it
at each loop iteration:

% This makes values2 become a 1-by-10 vector all of whose elements are 0
values2 = zeros(1, 10);
% Now instead of causing it to grow we're just changing a 0 to another
number
for k = 1:10
    values2(k) = k+1;
end

Alternately, we can dispense with the loop entirely and use a vectorized
approach, operating on all the elements of the vector at once instead of one
at a time.

values3 = 1:10;
values3 = values3 + 1;
% or
values4 = (1:10)+1;

Using this technique, you should be able to modify your code to treat x, y,
and z as vectors that you can then PLOT outside the loop.

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

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