How to vary color on single 2d line generated using a 'for loop' and conditional statements?

I have completed a script that runs a range of x values through for loops that generates various outputs correlated to the conditional statements and plots them. How would I allocate 3 various colors to the curve dependent on which condition was used to generate that section of the curve? Is it possible to define the color of a curve over a specific domain or x value? I have read over a question previously posted for varying color on 2d lines but was unable to duplicate it. I have included my current script below. Thank you for any insight or direction!
clc; clear all;
x=-2:.01:6;%x defined
for k=1:length(x);%for loop initiated and k defined
if x(k)<-1;%first conditional statment
y(k)=exp(x(k)+1);%resulting statement if condition1 is met
elseif (x(k)>=-1)&(x(k)<5);%2nd condition
y(k)=2+cos(pi*x(k));%resulting statement if condtion2 is met
else
y(k)=10*(x(k)-5)+1;%resulting statement if condition3 is met
end
end
plot(x,y),xlabel('Time (SECONDS)'),ylabel('Height (KM)')

 Accepted Answer

line() and lineseries() objects must be a single color for their entire length. You would need to break into distinct lines every time the color changed.
Before you break things into lines, you need to decide what portion should be colored what.
If the first two points are in the first condition, then you probably want the line segment between them to be the first color, right?
Okay, now if the third point is the second condition, then what color should be used for where? This is the segment joining point 2 (generated by condition 1) with point 3 (generated with condition 2). Should it be the color of the originating point, condition 1? Should it be the color of the destination point, condition 2? Should it switch half-way between the two from the color for condition 1 to the color to condition 2? Should the vertices be colored instead of the lines? Should the line segment colors then be determined by interpolation?
Question: for the purpose of this code, are we allowed to make the assumption that x will be strictly increasing, or could x sometimes go backwards?

5 Comments

The assumption that x is strictly increasing is correct. I am not picky about which colors are used, I will create a correlated legend. I believe that line color would be most effective at distinguishing the various functions utilized to create said portions of the curves. There may possibly be a more effective way to create such a distinction but this is what came to my mind after some reading and trials. I am open to other methods of distinction.
I believe valid points for colors would be:
color1= ..-# to -1.01, color2= -1 to 4.99, color3= 5 to +#...
You should find it relatively easy to implement if you convert your "for" loop into logical indexing. The three resulting logical index vectors can then be used for plotting:
plot( x(LogVec1), y(LogVec1), 'r', x(LogVec2), y(LogVec2), 'g', x(LogVec3), y(LogVec3), 'b')
you will need to decide what you want to do about connecting the last of each range with the first of the next.
If I were to convert my for loop into logical indexing would I be able to input a range of 'x's and have the script process it for the various conditional statements or would I have to differentiate my inputs for the various functions? Is there currently a FAQ section or previous post on converting for loop to a logical indexing on the site?
x = linspace(....);
y = zeros(size(x));
index1 = (x < -1);
index2 = (x < 5) & (~index1);
index3 = ~(index1 | index2);
y(index1) = f1(x(index1)); %first formula
y(index2) = f2(x(index2)); %second formula
y(index3) = f3(x(index3)); %third formula
plot( x(index1), y(index1), 'r', x(index2), y(index2), 'g', x(index3), y(index3), 'b')

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!