title: Changing color of plot lines in a loop
body: <p>I have a question about changing the color of lines in a plot in MATLAB. I have written a code where I want to change the color of lines in a plot that is embedded in several for loops (the code is shown below). In the code, a new plot is created (with its own figure) when the "if loop" condition is satisfied. I want each plot to have lines with different colors (from other plots), so I created a variable = "NewColor" to increment and change the line colors. However, the following problem has been occurring:</p>
  
  <p>Suppose I am in debug mode and I have stopped at the plot command. I run the next step and a plot is created with a blue line. I check the value of NewColor and find NewColor = 0.1. Next, I use the "run to cursor" command to step to the next time the plot command is activated. When I do this I am still within the "i for loop", so NewColor has not changed. I check the editor to find that NewColor = 0.1. Therefore, when I run the next step a blue line should show up on the current plot. To the contrary and my disbelief an orange line shows up (in addition to the blue line). I don't understand since in both steps of the debugger NewColor = 0.1, and the code is written so the color of the lines = [0,NewColor,0]. If anyone can find the error of my ways it would be greatly appreciated. Thanks </p>
  
  <pre><code>ThetaPlot = [40,50];   % Put incident angle as input
  
  Count1 = 0;
  Count2 = 0;
  NewColor = 0;
  
  for m = 1:length(ThetaPlot)
      NewColor = 0.1;
      Title = sprintf('Angle(%d)',ThetaPlot(m));
      figure('name',Title)
      Count1 = 0;
      for i = 1:length(xrange)-1             % X Coordinate of Start Node
          for j = 1:length(yrange)-1         % Y Coordinate of Start Node
              Count1 = Count1+1;
              for k = 2:length(xrange)       % X Coordinate of End Node
                  for l = 2:length(yrange)   % Y Coordinate of End Node
                      Count2 = Count2+1;
                      if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
                          x = [xrange(i),xrange(k)];
                          y = [yrange(j),yrange(l)];
                          plot(x,y,['-',[0,NewColor,0],'o']);
                          hold on;
                      end
                  end
                  Count2 = 0;
              end
          end
      end
      NewColor = NewColor + 0.02;
  end
  </code></pre>
  
  <p>Full Code:</p>
  
  <pre><code>%% Calculating Angles of Reflection
  
  run = 1; % Set run = 1 for calculations
  
  if run == 1
      xrange = [0:1:14.5]';      % Coordinates to try for Panel Geometry (in)
      yrange = [0:1:36]';        % Coordinates to try for Panel Geometry (in)
      ThetaIncident = [-90:1:90]';     % Incident Angle of Ray (measured relative to normal direction with clockwise postive)
      OvenGlassXrange = [14.5:0.1:36.5]; %Range of X coordinates for Oven Glass
  
  ReflRayData = zeros((length(xrange)-1)*(length(yrange)-1),length(ThetaIncident),(length(xrange)-1)*(length(yrange)-1));  % Matrix containing Reflected Ray Data
  
  Count1 = 0;
  Count2 = 0;
  for i = 1:length(xrange)-1             % X Coordinate of Start Node
      for j = 1:length(yrange)-1         % Y Coordinate of Start Node
          Count1 = Count1+1;
          for k = 2:length(xrange)       % X Coordinate of End Node
              for l = 2:length(yrange)   % Y Coordinate of End Node
                  Count2 = Count2+1;
                  for m = 1:length(ThetaIncident)
                      xStart = xrange(i);
                      yStart = yrange(j);
                      xEnd = xrange(k);
                      yEnd = yrange(l);
  
                      m1 = (yEnd - yStart)/(xEnd - xStart);  % Slope between Start and End Nodes
                      b1 = yStart - m1*xStart;
  
                      m2 = 1/m1;                             % Slope of normal direction
  
                      b2 = (yEnd - 0.5*(yEnd - yStart)) - m2*(xEnd - 0.5*(xEnd - xStart));
  
                      ArbXCoor = 1;                          % Arbitary Point X Coordinate on Normal Line
                      ArbYCoor = m2*ArbXCoor+b2;             % Arbitary Point Y Coordinate on Normal Line
  
                      ThetaReflected = -ThetaIncident(m);       % Reflected Angle
  
                      ArbXCoorRot = ArbXCoor*cosd(ThetaReflected) - ArbYCoor*sind(ThetaReflected); % Arbitary Point X Coordinate on Reflected Line
                      ArbYCoorRot = ArbYCoor*cosd(ThetaReflected) + ArbXCoor*sind(ThetaReflected); % Arbitary Point Y Coordinate on Reflected Line
  
                      m3 = (ArbYCoorRot - (yEnd - 0.5*(yEnd - yStart)))/(ArbXCoorRot - (xEnd - 0.5*(xEnd - xStart))); % Slope of Reflected Line
                      b3 = (yEnd - 0.5*(yEnd - yStart)) - m3*(xEnd - 0.5*(xEnd - xStart));
  
                      ElemLength = sqrt((yEnd - yStart)^2 + (xEnd - xStart)^2);
  
                      if min(OvenGlassXrange) &lt; -b3/m3 &amp;&amp; -b3/m3 &lt; max(OvenGlassXrange) &amp;&amp; -1 &lt; m1 &amp;&amp; m1 &lt; 0 &amp;&amp; m1 ~= -Inf &amp;&amp; m1 ~= Inf &amp;&amp; ElemLength &lt; 3
                          ReflRayData(Count2,m,Count1) = 1;
                      end 
                  end
              end
          end
          Count2 = 0;
      end
  end
  
  %% Plotting
  
  ThetaPlot = [40,50];   % Put incident angle as input
  
  Count1 = 0;
  Count2 = 0;
  NewColor = 0;
  
  for m = 1:length(ThetaPlot)
      NewColor = 0.1;
      Title = sprintf('Angle(%d)',ThetaPlot(m));
      figure('name',Title)
      Count1 = 0;
      for i = 1:length(xrange)-1             % X Coordinate of Start Node
          for j = 1:length(yrange)-1         % Y Coordinate of Start Node
              Count1 = Count1+1;
              for k = 2:length(xrange)       % X Coordinate of End Node
                  hold on;
                  for l = 2:length(yrange)   % Y Coordinate of End Node
                      Count2 = Count2+1;
                      if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
                          x = [xrange(i),xrange(k)];
                          y = [yrange(j),yrange(l)];
                          plot(x,y,['-',[0,NewColor,0],'o']);
                          hold on;
                      end
                  end
                  Count2 = 0;
              end
          end
      end
      NewColor = NewColor + 0.02;
  end
  </code></pre>