"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(s43);
>> > m = magnitude;
>> > theta = ((s44)*.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
>> overwrite x, y, and z on each pass, then attempt a 3D 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 1by10 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 1by10 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
