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:
Problems with plotting lines and curves

Subject: Problems with plotting lines and curves

From: Andrew Cross

Date: 11 Mar, 2010 21:30:25

Message: 1 of 2

Hi,

I'm fairly new to Matlab and am having some problems with plotting functions.
I've got two functions that I want to plot. One is a line, the other is a negative exponential curve. Both functions are calculated with depth simultaneously, but I only want to display one of the functions at each given depth (i.e. when the line function has a value less than that of the curve, I want it to plot the line, but when the curve becomes less than the line, I want to only plot the curve).

If I do this within the for loop, it only plots the points at which depth is calculated (h = 0:0.1:40) - is there any way to join these points to form a solid line? The code is:

   if (var2(:,n) < var3(:,n))
       plot(var2(n),h(n),'linewidth',1);
       legend('Straight line');
   else
       plot(var3(n),h(n),'r','linewidth',1);
        legend('Curve');
   end

Otherwise, if I place the plot function outside the for loop, the 'if' function doesn't work and only plots whichever function is least at the maximum depth.

Any help really would be very much appreciated!

Cheers,

Andrew

Subject: Problems with plotting lines and curves

From: Sadik

Date: 12 Mar, 2010 00:51:04

Message: 2 of 2

Hi Andrew,

Why not collect the line and the curve in two separate vectors? You should also collect the corresponding depth info so that you will say:

plot(hLine,linePoints)
hold on
plot(hCurve,curvePoints,'r')

or something like that.

Best.


"Andrew Cross" <butross@hotmail.com> wrote in message <hnbndh$2uk$1@fred.mathworks.com>...
> Hi,
>
> I'm fairly new to Matlab and am having some problems with plotting functions.
> I've got two functions that I want to plot. One is a line, the other is a negative exponential curve. Both functions are calculated with depth simultaneously, but I only want to display one of the functions at each given depth (i.e. when the line function has a value less than that of the curve, I want it to plot the line, but when the curve becomes less than the line, I want to only plot the curve).
>
> If I do this within the for loop, it only plots the points at which depth is calculated (h = 0:0.1:40) - is there any way to join these points to form a solid line? The code is:
>
> if (var2(:,n) < var3(:,n))
> plot(var2(n),h(n),'linewidth',1);
> legend('Straight line');
> else
> plot(var3(n),h(n),'r','linewidth',1);
> legend('Curve');
> end
>
> Otherwise, if I place the plot function outside the for loop, the 'if' function doesn't work and only plots whichever function is least at the maximum depth.
>
> Any help really would be very much appreciated!
>
> Cheers,
>
> Andrew

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